Created
March 11, 2013 15:55
-
-
Save carlossalas/5135243 to your computer and use it in GitHub Desktop.
JS: LimitMaxLength
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//LimitMaxLength | |
jQuery.fn.limitMaxlength = function(options){var settings = jQuery.extend({attribute: "maxlength", onLimit: function(){}, onEdit: function(){} }, options); 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)); jQuery.proxy(settings.onLimit, this)(); } jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length); } this.each(onEdit); return this.keyup(onEdit) .keydown(onEdit) .focus(onEdit) .blur(onEdit) .on('input paste', onEdit); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment