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)

5+ Year Member

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 Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

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 Sep 21, 2008 (gmt 0)

10+ Year Member

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 Sep 23, 2008 (gmt 0)

10+ Year Member


Featured Threads

Hot Threads This Week

Hot Threads This Month