[Javascript] Maxlength on textarea in XHTML
Shawn Milo
shawn.milo at gmail.com
Thu Apr 14 12:58:22 CDT 2005
I found this page, which demonstrates one way of doing what you want:
http://javascript.internet.com/forms/limit-textarea.html
Here's a little something I threw together, initially based on your function.
I put the call into the body's onload, just to be sure that the textareas were
all in existance when the function ran.
Shawn
------------------------------------------------------------------------------------------------
<html>
<head>
<title>
Width Enforcement
</title>
<script type="text/javascript">
var maxWidth = 10;
function enforceMaxWidth(){
var x = document.getElementsByTagName('textarea');
for (var i=0;i<x.length;i++){
x[i].onkeypress = function(){ if (this.value.length >
maxWidth){this.value = this.value.substr(0, maxWidth); } }
}
}
</script>
</head>
<body onload="setTimeout('enforceMaxWidth()', 100);">
<form id="frmTest" method="submit" action="./maxLength.html">
<input type="text" id="text1" name="text1" value=""/>
<br/>
<textarea id="ta1" name="ta1" maxlength="1000"></textarea>
<br/>
<textarea id="ta2" name="ta2" maxlength="100"></textarea>
<br/>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------
More information about the Javascript
mailing list