Skip to content

Instantly share code, notes, and snippets.

@rbrenton
Created October 5, 2015 21:04
Show Gist options
  • Save rbrenton/e780c1c5e69233efa46f to your computer and use it in GitHub Desktop.
Save rbrenton/e780c1c5e69233efa46f to your computer and use it in GitHub Desktop.
jQuery to enforce maxlength for textarea elements.
// jQuery to enforce maxlength for textarea elements.
(function(){
var check = function(e) {
var max = $(e).attr('maxlength');
if (e.value.length > max) {
e.value = e.value.substr(0, max);
}
};
$('body').on('keydown', 'textarea[maxlength]', function(){ check(this); });
$('body').on('keyup', 'textarea[maxlength]', function(){ check(this); });
$('body').on('change', 'textarea[maxlength]', function(){ check(this); });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment