javascript: crawling the dom for menus
yep, I've been playing with javascript quite a bit. Anyway, I have a dynamically generated menu that looks kinda like this.
what I wanted to do was automatically put little graphics in the first and last tds on each line as you mouse over it, but I didn't want to write alot of code. You can see the little javascript that the menu requires... and since each line is dynamically generated it's really very little.
The javascript function is this:
first I set vars for the left and right side graphics, then assign the entire menu to a variable.
I then crawl the dom... I go to the menu's firstchild (which is the tbody tag), then iterate through the childNodes (the tr tags). If the tr tag is a number equal to what's passed into the function I set the left and right tds (childNodes 0 and 2) to the images, otherwise I unset them. Pretty hot! And, even better, it works in both Firefox and IE7.