Created
May 26, 2012 13:46
-
-
Save Schubidu/2794017 to your computer and use it in GitHub Desktop.
supports maxLength-limitations to textarea in prototypejs
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
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