JQuery: determine if <li> a <ul>

I have HTML code containing nested elements <ul>, and I need to add a parent class for each element <li>that contains a child element <ul>. There may be more than one element contained directly with one <li>, for example:

<li>
    <a>...</a>
    <span>...</span>
    <ul>...</ul>
</li>

All I need to do is determine if a particular <li>a contains <ul>as a child.

How can i do this?

+5
source share
3 answers

This will do what you need:

$('li:has(> ul)').addClass('parent');
+29
source

Changed the answer after re-reading the question:

$("li:has(ul)").addClass("parent");
+4
source

.

$("li:has(ul)").addClass("parent")
+4

All Articles