[thelist] Javascript Error

Andrew Clover and-evolt at doxdesk.com
Wed Dec 22 07:44:45 CST 2004


Matt Warden <mwarden at gmail.com> wrote:

> For that, you need something like this:

> for (element in window.document.megasearch.elements) {

Actually even that's not good enough. The 'elements' array has members 
such as 'length' and 'item', which you wouldn't want to iterate over.

The for...in loop in JavaScript doesn't iterate over Arrays in the way 
you might think (as it does in other languages), it only runs over the 
list of member names. This is of course of very little practical use, 
but then JavaScript has never been designed in anything like a sane way. 
You still have to use the old-fashioned indexed-iterator for-loop for 
this purpose.

Also you'd want to avoid 'document.megasearch' to retrieve the <form 
name="megasearch">; putting named forms into the Document object is 
non-standard and probably doesn't work on all browsers. 
'document.forms.megasearch' would be the 'correct' documented JS way of 
doing it; if you're using <form id="megasearch"> (and hence ignoring 
Netscape 4) you could also use document.getElementById('megasearch').

   var elements= document.forms.megasearch.elements;
   for (var i= 0; i<elements.length; i++) {
     var element= elements[i];
   }

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/


More information about the thelist mailing list