Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Forked from nodesocket/gist:3102236
Last active December 14, 2015 21:19
Show Gist options
  • Save cpilsworth/5150617 to your computer and use it in GitHub Desktop.
Save cpilsworth/5150617 to your computer and use it in GitHub Desktop.
Listen for textarea changes to textarea and limit length according to maxlength attribute
$(document).on('keyup input paste', 'textarea[maxlength]', function() {
var limit = parseInt($(this).attr('maxlength'));
var text = $(this).val();
var chars = text.length;
if (chars > limit) {
var new_text = text.substr(0, limit);
$(this).val(new_text);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment