Skip to content

Instantly share code, notes, and snippets.

@joecritch
Created May 28, 2010 08:54
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 joecritch/416923 to your computer and use it in GitHub Desktop.
Save joecritch/416923 to your computer and use it in GitHub Desktop.
// Limit character lengths on textareas using jQuery and metadata
// ////////////////////////////
// Store the metadata in data() for quicker access on the event.
var textboxesToLimit = $('textarea.character_limit');
textboxesToLimit.each(function() {
$(this).data('maxlength', $(this).metadata().maxlength);
}).keyup(function() {
var len = this.value.length;
var maxlength = $(this).data('maxlength');
if (len >= 150) {
this.value = this.value.substring(0, maxlength);
}
$(this).nextAll('.charsLeft').text(maxlength - len);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment