Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.