Skip to content

Instantly share code, notes, and snippets.

@Schubidu
Created May 26, 2012 13:46
Show Gist options
  • Save Schubidu/2794017 to your computer and use it in GitHub Desktop.
Save Schubidu/2794017 to your computer and use it in GitHub Desktop.
supports maxLength-limitations to textarea in prototypejs
Element.addMethods(["textarea"], {
/**
* @description supports maxLength-limitations to textarea;
* @param element
* @param max charset length in field
* @param handler a handler-function which have the current left charset value in parameter
*/
maxTextLength: function(element, max, handler){
element = $(element);
handler = (handler == undefined) ? function(){} : handler;
var checkLength = function(){
if( this.value.length >= max ) {
this.value = element.value.substring(0, max);
handler(0);
} else {
handler(max - this.value.length);
}
};
element.observe('keyup', checkLength.bindAsEventListener(element));
checkLength.bindAsEventListener(element);
return element;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment