473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

referencing an array of <div> elements in firefox

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="showOr Hide('menu[0]')"> show/hide menu1 </span>
<span onclick="showOr Hide('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.getEle mentById(), 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.

Jul 23 '05 #1
5 13146
I think you are prematurely forgiving on firefox
for existing implementations undiscovered.

ECMA scripts are basically javascript and that [ECMA] is

available with all latest browsers, let's say top 4.

"'arrays' proprietary to IE?I"

NO, do you homework.

<hi*******@digi verse.net> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
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="showOr Hide('menu[0]')"> show/hide menu1 </span>
<span onclick="showOr Hide('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.getEle mentById(), 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.


Jul 23 '05 #2
wrote:
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="showOr Hide('menu[0]')"> show/hide menu1 </span>
<span onclick="showOr Hide('mennu[1])"> show/hide menu2 </span>
The id attribute must be unique in your document. You are not allowed to
have two tags with the same id. Give your div tags each a separate id:

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

<span onclick="showOr Hide('menu1')"> show/hide menu1 </span>
<span onclick="showOr Hide('menu2')"> show/hide menu2 </span>

function showOrHide(id){
var myObject = eval(id);
if( myObject.style. display == 'none') myObject.style. display =
'block';
else myObject.style. display = 'none';
}
Don't use eval here, there are very few situations where you should even
consider using eval, and this isn't one of them. Use the methods supplied
for accessing elements: getElementById is what you want.

Don't set the div's style explicitly to block: you are better to clear the
overriding style so that you get the default style back again (then you can
use the same function on inline or table elements), or you could set a
className.

function showOrHide(id){
var myObject = document.getEle mentById(id);
if (myObject) {
myObject.style. display = 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)


Simply following the standards will get a lot of your code working on
multiple browsers without any special API. The best thing you can do is to
develop first for browsers which are closer to being standards compliant
(e.g. Mozilla/Firefox), and then treat IE as the exception. Apart from
anything else this means the initial development will be much easier as you
will have access to decent debugging tools.

If you wish you can add checking for support for getElementById, but that
depends on how desperate you are to support users of extremely old
browsers.
Jul 23 '05 #3
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. :o)

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

Barend.

Jul 23 '05 #4
wrote:
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. :o)


If you want to do the equivalent in a standard compliant way then you
can give your div elements a class.

<div class="menu"> menu1 </div>
<div class="menu"> menu2 </div>
<div class="menu"> menu3 </div>
<div class="menu anotherclass"> menu4 </div>

<span onclick="showOr Hide(menus, 0)"> show/hide menu1 </span>
<span onclick="showOr Hide(menus, 1)"> show/hide menu2 </span>

And then in your script:

function getElementsByBa seTagClass(base , tag, className) {
var classPat = new RegExp('\\b'+cl assName+'\\b');
var nodes = base.getElement sByTagName(tag) ;
var matching = [];
for (var i = 0; i < nodes.length; i++) {
if (classPat.test( nodes[i].className)) {
matching.push(n odes[i]);
}
}
return matching;
}
var menus = getElementsByBa seTagClass(docu ment.body, 'div', 'menu');

function showOrHide(tags , index){
if (tags && index < tags.length) {
var style = tags[index].style;
style.display = style.display ? '' : 'none';
}
}

The main disadvantage is this could be slowish if you have a lot of tags to
search which is why it may be best to precalculate the array. Remember also
that you can give a tag several classes, so this doesn't stop you using
class to style your div's as well as using it for lookup.

The other drawback is that the assignment to menus has to be done after the
page has loaded, so either in a window.onload function or in inline
<script> at the end of the page (but if it is inline you can't put the
functions in a separate file, so don't do that).
Jul 23 '05 #5
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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3518
by: Matthew | last post by:
Hello! What is the best NS/IE compliant way to accomplish this using the least amount of javascript code? <div id="block1"> Group 1 Code </div> <div id="block2"> Group 2 Code </div>
23
4049
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
1
3895
by: Alan Hoyle | last post by:
I was using a <table border> to generate borders around some info/images, and decided to follow the w3c guidelines and convert it to CSS boxes with borders since it wasn't really tabular data. It used to be something like this: <table> <tr> <td><img>caption</td> <td><img>caption</td>
3
11425
by: Chantal | last post by:
Hello everybody, I'm relatively new to javascript + DOM so I've a question : I'm removing <div> elements in DOM with javascript. It works perfectly in Firefox and Opera : I mean the DOM is refreshed automatically and I see the changes. But in IE (version 6), I don't see any changes. Do I have do to something more with IE to tell "him"...
2
2952
by: Nikolai Onken | last post by:
Hey, I am trying to learn the exact behavior of CSS and was just placing a couple of <div>'s after each other. Each of the <divhas a <pwithin and some text within the <p> Now when I add a background-color to the div, the color won't fill up the entire space I believe it should. The div only has color around the text though it has one line...
28
5327
by: Kent Feiler | last post by:
1. Here's some html from a W3C recommendations page. <P>aaaaaaaaa<DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</DIV> 2.Although I didn't think it would make any difference, I tried it with the </p>s included as well. <P>aaaaaaaaa</p><DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</p></DIV>
5
3285
uranuskid
by: uranuskid | last post by:
Hey guys, I control my webappearance ewith a series of <div> elements. They are supposed to be in line and the main container is supposed to stay min 880px in wdth. However, the left infobox has somehow the bad habit to start under the main <div> element. Also, the min-width is not kicking in, instead the elements get floated to the left and no...
2
3196
by: paul | last post by:
I have a JS function to change the width of a <divthat works great in Firefox, but not at all in IE7. In IE an error message occurs: Line: 92 Char: 5 Error: Invalid Argument Code: 0 Firefox reports no errors in the Error Console, or Firebug, and the <divis resized correctly. Here is the function:
8
10021
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.