referencing an array of <div> elements in firefox

H

hibernate

I'm somewhat new to javascript/DHTML, and this problem has been
plaguing me. I have made an 'array' of <div> tags within my html
document like so:

<div id="menu"> menu1 </div>
<div id="menu"> menu2 </div>
<div id="menu"> menu3 </div>
<div id="menu"> menu4 </div>

<span onclick="showOrHide('menu[0]')"> show/hide menu1 </span>
<span onclick="showOrHide('mennu[1])"> show/hide menu2 </span>


I want to be able to show/hide a menu by clicking on the specified
area...
in IE6 this is easily done with this javascript:

function showOrHide(id){
var myObject = eval(id);
if( myObject.style.display == 'none') myObject.style.display =
'block';
else myObject.style.display = 'none';
}

this allows me to hide/show the menus as needed (my code is a little
more complicated, including an 'API' to make my page 'work' on multiple
browsers, but this is the basic idea in essence)

what is the correct way to reference the <div> array in firefox?? I've
tried multiple ways such as document.getElementById(), etc... but I
just can't figure it out, is it even possible? are these element
'arrays' proprietary to IE? I know I'll feel really lame when I find
out, but i've searched for hours and haven't found anything close to an
answer.
 
H

hibernate

Thank you Rleib, I need to look further into that ECMA, I think your
comment answers the question of why IE6 treats duplicate tag IDs as
arrays while Mozilla does not. I am aware that 'arrays' in general are
not proprietary to IE. ;o)

Thank you Duncan for your comment too. I do appreciate the debuging
tools available to mozilla/firefox, it's not even worth comparing to
the excuse of a debugger that IE6 offers. So I develop on Firefox first
(lesson learned relatively quickly there. lol), however I discovered by
accident that duplicate ID's can produce arrays in IE, and I was trying
to take advantage of that feature (and it's length property). That
example was a quick (dirty) example that I knew would work - to convey
the idea. :eek:)

Thanks for the help! I guess I'll stop looking for shortcuts and go do
it the right way now...

Barend.
 
B

Barend

I have devised a system similar to the way you pointed out. I used
dates instead of a regular expression, since the menu names are roughly
based on dates and everything I need can be calc'd from that.. this
worked for me, and is probably a little less intensive to process (just
a guess). Thanks for the mention of the window.onload function, I ran
into a problem with calling an onload from the <body>, this solved it
perfectly.

Thanks for the help!

Barend
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top