[thelist] Javascript for/in

Peter-Paul Koch ppk at xs4all.nl
Sat Oct 21 08:00:01 CDT 2000


>Why won't this bit of JavaScript cause all the checkboxes in 'MainTableForm'
>to be checked when run?  I've tried every variation of the script below that
>I can think of.  I can do it with a regular for loop, but I want the for/in
>loop to work like it's supposed to.
>
>for (checkbox in document.forms['MainTableForm'])
>			{
>			document.forms['MainTableForm'].elements[checkbox].checked=t>rue;
>			}

for...in is meant to read out *everything* about the form, so also the
ACTION, METHOD, TARGET, all other stuff. In addition, it contains the array
'elements' but not the individual elements (though in NN4 it does contain
the elements).

This is simply not the way to use for...in . I only use it when I want to
know *all* the methods and properties of a certain object. Its value in
actual scripts is slight. Use a normal for loop instead.

ppk




More information about the thelist mailing list