View Full Version : Use JS to select a li inside a li

04-21-2004, 03:51 AM
How could I use JS to select a <ul> element which is inside a <li> element?

04-21-2004, 04:32 AM
You can use getElementsByTagName from any root element, so if you start from your list-item then the nested <ul> will be [0] in that array:

var submenu = listItem.getElementsByTagName('ul')[0];

Assuming you know it will be there. If you don't then you can query the length property of that collection before grabbing the object from it, or not:

var menus = listItem.getElementsByTagName('ul');
var submenu = (menus.length > 0) ? menus[0] : null;

04-21-2004, 01:12 PM
the listItem, would this be something like:

document.getElementById('Menu') cause that doesnt work for me, :(

04-21-2004, 02:12 PM
I give up:

Brother cake, how did you manage to build you udm on this basis?:

<li><a href="#">lorem</a></li>
<li><a href="#">lorem</a></li>
<li><a href="#">lorem</a></li>
<a href="#">adipiscing</a>
<li><a href="#">dolor</a></li>
<li><a href="#">elit</a></li>
<li><a href="#">ipsum</a></li>
<li><a href="#">Donec</a></li>

thanks alot, I have been trying to select the UL with js, when i hover over the LI, but then the A tag gets in the way (which is in the LI tag), but then i no longer hover over a li tag, i hover of the a tag...

soo please tell me how you manage to do it....

thanks alot :D

04-25-2004, 09:20 AM
Well have you seen the prototype I made at http://www.brothercake.com/scripts/listmenu/ ? That'll give you the basic event handling model.

04-25-2004, 03:49 PM
thanks, I am working on MY menu right now, I thought it can do everything yours can do but then i saw that i have about 1/10000 of your code, about 10 lines of JS, I MUST be missing something :D

....dude, very nice menu :D, love it.....

04-25-2004, 06:22 PM
Nah that link is to a much earlier version - it only has a dozen lines of scripting in it. I only suggested you look at it to see how I bound the event handlers to the list items and managed the flow of events.

04-25-2004, 10:00 PM
Nah, thanks alot brothercake, but that script is way over my head :D