RobertD RobertD - 3 months ago 13
Javascript Question

select li element based on the ul

how I can select the second ul li element, I have this structure:
HTML

<ul>
<li></li>
<ul>
<li>I want to select this</li>
</ul>
</ul>

Answer

Firstly your HTML is invalid. You cannot have ul as a child of another ul. It must be within an li. Also the text within the ul must also be within a li.

<ul>
    <li>
        <ul>
            <li>I want to select this</li>
        </ul>
    </li>
</ul>

To then select this in jQuery you can use:

$('ul > li > ul > li:first');

You could also use the less strict: ul ul li:first, depending on how rigidly you need to adhere to the HTML structure you defined.