Created
April 8, 2010 14:40
-
-
Save BRMatt/360132 to your computer and use it in GitHub Desktop.
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
jQuery.fn.limitMaxlength = function(options){ | |
settings = jQuery.extend({ | |
attribute: "maxlength", | |
onInit: function(){}, | |
onLimit: function(){}, | |
onEdit: function(){} | |
}, options); | |
// Event handler to limit the textarea | |
var onEdit = function(){ | |
var textarea = jQuery(this); | |
var maxlength = parseInt(textarea.attr('maxlength')); | |
if(textarea.val().length > maxlength){ | |
textarea.val(textarea.val().substr(0, maxlength)); | |
jQuery.proxy(settings.onLimit, this)(); | |
} | |
jQuery.proxy(settings.onEdit, this)(); | |
} | |
this.each(settings.onInit); | |
return this.keyup(onEdit) | |
.focus(onEdit) | |
.focusout(onEdit); | |
} |
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
$(document).ready(function(){ | |
var onEditCallback = function(){ | |
var length = $(this).val().length; | |
var maxlength = parseInt($(this).attr('maxlength')); | |
$(this) | |
.siblings('#charsRemaining') | |
.text('Characters remaining ' + (maxlength-length)); | |
} | |
$('textarea[maxlength]').limitMaxlength({ | |
onInit: onEditCallback, | |
onEdit: onEditCallback, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Silvialenciu You have my permission to copy, use, modify, or redistribute the code in any way without attributing me.