[Javascript] Maxlength on textarea in XHTML

Peter-Paul Koch pp.koch at gmail.com
Sun Apr 17 11:19:51 CDT 2005


> 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);
> }

This is incorrect. Now the checkLength function is *executed* for
every textarea onLoad, and the result is assigned to the keypress
event. Correct:

x[i].onkeypress = checkLength;

Now you don't execute the function; instead you assign it to the
keypress event handler, and it waits until the user actually presses a
button before executing.
 
-- 
-------------------------------------------------------------------
ppk, freelance web developer

Bug Report: Report bugs yourself, or comment on previously 
reported ones.
http://www.quirksmode.org/bugreports/
------------------------------------------------------------------



More information about the Javascript mailing list