matbergman
02-22-2006, 06:31 PM
I want to simplify my markup by elimating IDs, and relying on traversing the document tree to detect the presence of specific tags.
I am creating a flyout navigation with a set of nested HTML lists, CSS, and a bit of JavaScript. My protype functions fine, and the HTML that generates the navigation (sans styles and scripts) looks something like this:
<ul>
<li onMouseOver="flyout('flyout_1');"><a href="#">Navigation item 1</a>
<ul id="flyout_1">
<li><a href="#">Navigation item 1.1</a></li>
<li><a href="#">Navigation item 1.2</a></li>
</ul>
</li>
<li><a href="#">Navigation item 2</a></li>
</ul>
This example relies on the getElementById() method to display the nested <ul>. It works by passing the ID of the hidden list in the "flyout" function so that JavaScript can control the DISPLAY property of that element. I want to eliminate the IDs in the tags, and instead identify nested lists by traversing the document's nodes and utilizing the getElementsByTagName() method. Ideally, if a list item is moused over, the script would detect if a child <ul> tag exists, and if so, set the child list's display from "none" to "block".
Here's some non-functioning pseudo-code to suggest what I'm trying to accomplish:
var children = document.getElementsByTagName("ul").childNodes;
for (i=0; i<children.length; i++) {
if (children[i]=="ul") {display the list}
}
Of course, my existing script that utilizes the getElementsById() method works fine. I've built several of these navigations for my clients, but I'm always looking for ways to streamline the markup aspect of my projects, and traversing the document tree seems to be a potentially powerful solution. I have accomplished something similar in XSLT before, but my attempts to do the same in JavaScript have not been successful. Is my goal possible?
I am creating a flyout navigation with a set of nested HTML lists, CSS, and a bit of JavaScript. My protype functions fine, and the HTML that generates the navigation (sans styles and scripts) looks something like this:
<ul>
<li onMouseOver="flyout('flyout_1');"><a href="#">Navigation item 1</a>
<ul id="flyout_1">
<li><a href="#">Navigation item 1.1</a></li>
<li><a href="#">Navigation item 1.2</a></li>
</ul>
</li>
<li><a href="#">Navigation item 2</a></li>
</ul>
This example relies on the getElementById() method to display the nested <ul>. It works by passing the ID of the hidden list in the "flyout" function so that JavaScript can control the DISPLAY property of that element. I want to eliminate the IDs in the tags, and instead identify nested lists by traversing the document's nodes and utilizing the getElementsByTagName() method. Ideally, if a list item is moused over, the script would detect if a child <ul> tag exists, and if so, set the child list's display from "none" to "block".
Here's some non-functioning pseudo-code to suggest what I'm trying to accomplish:
var children = document.getElementsByTagName("ul").childNodes;
for (i=0; i<children.length; i++) {
if (children[i]=="ul") {display the list}
}
Of course, my existing script that utilizes the getElementsById() method works fine. I've built several of these navigations for my clients, but I'm always looking for ways to streamline the markup aspect of my projects, and traversing the document tree seems to be a potentially powerful solution. I have accomplished something similar in XSLT before, but my attempts to do the same in JavaScript have not been successful. Is my goal possible?