I would like to add a class called “with_ul” to li tags where there is ul under in the following normal list with jquery.
In the following case I need to add the class to list of Level 1-B, Level 2-B-1 and Level 1-C since they have an unordered list.
<ul id="nav">
<li>Level 1-A</li>
<li>Level 1-B
<ul>
<li>Level 2-B-1
<ul>
<li>Level 3-B-1</li>
</li>
<li>Level 2-B-2</li>
</ul>
</li>
<li>Level 1-C
<ul>
<li>Level 2-C-1</li>
</ul>
</li>
...
...
</ul>
Thanks in advance.
You can use the
:has()selector to select all elements that contains a specific descendant.For example:
If you only want to select<li>tags that directly contain a<ul>, you could write