Feature request: missing maxlength attribute support for <textarea>
unwiredbrain opened this issue · comments
Can you add support for the maxlength attribute when applied on textarea elements, please?
Current state of support: http://www.wufoo.com/html5/attributes/03-maxlength.html
Here's a shim I wrote (based on jQuery though)
/*!
* Textarea maxlength attribute shim
* Copyright (c) 2013 Massimo Lombardo. @unwiredbrain
* Licensed under the terms of the Apache License, version 2.0
* Inspired by: http://viralpatel.net/blogs/set-maxlength-of-textarea-input-using-jquery-javascript/
*/
if (!("maxLength" in document.createElement("textarea"))) {
$("textarea[maxlength]").each(function () {
var $this = $(this).on("keydown keyup focus blur", function (event) {
var keyCode = event.which,
that = this;
if (keyCode === 13 || keyCode >= 33) {
if (that.value.length >= limit) {
that.value = that.value.substr(0, limit - 1);
event.preventDefault();
}
}
}),
limit = Math.abs(parseInt($this.attr("maxlength"), 10)) || -1;
});
}
308 bytes once minified (according to Uglify.js)
Here's a test case: http://jsfiddle.net/wfthh/
Tried on IE 9.