Skip to content

Instantly share code, notes, and snippets.

@lscott3
Created October 4, 2012 19:02
Show Gist options
  • Save lscott3/3835702 to your computer and use it in GitHub Desktop.
Save lscott3/3835702 to your computer and use it in GitHub Desktop.
maxlength.js
jQuery.fn.limitMaxlength = function(options){
var settings = jQuery.extend({
attribute: "maxlength",
onLimit: function(){},
onEdit: function(){}
}, options);
// Event handler to limit the textarea
var onEdit = function(){
var textarea = jQuery(this);
var maxlength = parseInt(textarea.attr(settings.attribute));
if(textarea.val().length > maxlength){
textarea.val(textarea.val().substr(0, maxlength));
// Call the onlimit handler within the scope of the textarea
jQuery.proxy(settings.onLimit, this)();
}
// Call the onEdit handler within the scope of the textarea
jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
}
this.each(onEdit);
return this.keyup(onEdit)
.keydown(onEdit)
.focus(onEdit)
.live('input paste', onEdit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment