[Javascript] Maxlength on textarea in XHTML
Charles Stuart
about-css at enure.net
Thu Apr 14 11:14:49 CDT 2005
Hello All,
I need to set a maxlength on all of the textarea's on a site. I am
using XHTML 1.0 transitional. I've tried without success to implement
Peter-Paul Koch's code located here:
http://www.alistapart.com/authors/peterpaulkoch/
My needs are simpler than what is presented on A List Apart. The
maxlength will always be the same value. And, don't mind having
onkeypress="checkLength()"
in the XHTML.
So far I have this:
var x = document.getElementsByTagName('textarea');
for (var i=0;i<x.length;i++)
{
x[i].onkeypress = checkLength(x);
}
function checkLength()
{
var howMany = document.getElementsByTagName('textarea');
var max = 30;
if (howMany.length > max)
{
window.alert("You can only type 30 characters");
}
}
My second attempt was this:
function checkLength()
{
event = window.event;
element = event.srcElement;
var howMany = document.forms[0].element.length;
if (howMany.length > 30)
{
window.alert("You can only type 300 characters");
}
}
Thank you for any help. Let me know if I should put an example up.
best,
Charles
More information about the Javascript
mailing list