How do I check the tag of a child?
I'm trying to teach myself more complex javascript by making a cascading menu. What I've run into is trying to determine what the first child tag is. For example:
HTML Code:
<ul id="menu">
<li>Item 1
<ul>
<li>A</li>
<li>B</li>
</ul>
</li>
</ul> The tidbit of code I have at the beginning reads:
HTML Code:
function hideLists(){
var ulArray=menu.getElementsByTagName('ul');
for(var i=0;i<ulArray.length;i++){ulArray[i].style.display='none';}
}
And there's an onload in the body tag which quite nicely hides the nested ul. What I need to know is how to check each li tag in the list to see if it's first child is a ul tag, so I can then start on writing how I want it to display that ul.
I know the solution I'm looking for will use .childNodes, but I'm not terribly clear on how to make it happen.
HTML Code:
function hideLists(){
var ulArray=menu.getElementsByTagName('ul');
for(var i=0;i<ulArray.length;i++){ulArray[i].style.display='none';}
var liArray=menu.getElementsByTagName('li');
for(var i=0;i<liArray.length;i++){if(liArray[i].childNodes[0].nodeName='ul') liArray[i].style.cursor='pointer'; //Just to see if it recognizes the li's that have ul's as children.}
}
Can anyone offer a suggestion?
bryceowen, February 17th, 2009 02:33 PM
Bookmarks