Forum Moderators: open

Message Too Old, No Replies

for(element in document.all) in firefox

         

musicales

9:41 am on Nov 15, 2005 (gmt 0)

10+ Year Member



anyone know how to do the firefox equivalent of this :

for(element in document.all)
{
}

I'm trying to loop through all the ids in a document.
Thanks for any help.

Bernard Marx

11:06 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) You don't need to use document.all for V5 browsers,
use
document.getElementsByTagName('*')

Having said that, some versions of IE5 return a zro-length collection when

'*'
is used as an argument. So...

/* change corrupted ¦¦ chars for pipes */

var all = document.all [b][red]¦¦[/red][/b] document.getElementsByTagName('*');

That's the simplest way to ensure that you have a collection of all elements held in a variable.

2) Don't use a

for..in
loop on a collection of this kind.
Inconveniently enough, a
for..in
loop will also output the
length
property of the collection, which may well cause an error (depending on what you are doing with the output).

This construction is convenient:

for(var k=0, elm; elm=all[k++];)
{
// elm holds a reference to the element
}

musicales

11:52 am on Nov 15, 2005 (gmt 0)

10+ Year Member



Bernard - thanks. I had come across getElementsByTagName but couldn't find a way to get the ids from it. In your example would you expect it to be in elm or elm.id or something else?
Thanks

sorry - just worked it out - elm.id did the trick
Thanks