Skip to content

Instantly share code, notes, and snippets.

@simme
Created October 19, 2010 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simme/633912 to your computer and use it in GitHub Desktop.
Save simme/633912 to your computer and use it in GitHub Desktop.
Count characters
// Character counter
$('#edit-field-tagline-0-value, #edit-body').keydown(function(event) {
var id = $(this).attr('id');
var counter = $('label[for="' + id + '"]').children('span');
if (!counter.length) {
$('label[for="' + id + '"]').append('<span></span>');
counter = $('label[for="' + id + '"]').children('span');
}
counter.text(' (Antal tecken: ' + $(this).val().length + ')');
if ($(this).val().length > maxLength) {
if (event.keyCode !== 8) {
alert('Maxlängden för vald restaurangkategori är nådd.');
return false;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment