Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Loop to collect divs

5:45 pm on Sep 19, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:Oct 15, 2007
votes: 0

Trying to turn div visiblitly to false with a loop. When the user hovers over a menu item I call the function getMenuName(this). Passing "this" into the function because I have named my divs after the menu items preceded by "myDiv"

Here is what I have so far:

function getMenuName(obj)
var objName;
objName = "mydiv" + obj.childNodes[0].nodeValue;
objShow = document.getElementById(objName)
objShow.style.visibility = "visible"

This works great to make the desired div visible, but now I need to turn off the others. How do I loop through the page and...

1 - Collect all divs that begin with "myDiv"

2 - Change visibilty to hidden.

Thanks in advance.

2:41 am on Sept 20, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
votes: 0

You will need to collect all divs within page( or section in question).

var els = document.getElementsByTagName('div');

and check id of each.

2:35 am on Sept 21, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 25, 2002
posts: 470
votes: 0

I also strongly recommend looking into YUI (Yahoo's DHTML/AJAX framework), as they have a series of methods that work very well for these exact processes, for example:

var collection = YAHOO.util.Dom.getElementsByClassName('blah');

and "collection" will contain a collection of any object of class 'blah'.

8:15 pm on Sept 23, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Aug 7, 2002
votes: 0