Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: determine if a <li> contains a <ul>

Tags:

jquery

I have some HTML code that contains nested <ul> elements and I need to add a class 'parent' to each <li> element that contains a child <ul>. There could be more than one element contained directly withing a single <li>, e.g:

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

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

How would I go about this?

like image 634
Alfero Chingono Avatar asked Sep 23 '09 13:09

Alfero Chingono


3 Answers

This will accomplish what you need:

$('li:has(> ul)').addClass('parent');
like image 166
dcharles Avatar answered Nov 04 '22 19:11

dcharles


Changed the answer after re-reading the question:

$("li:has(ul)").addClass("parent");
like image 4
MiffTheFox Avatar answered Nov 04 '22 19:11

MiffTheFox


Use the has selector.

$("li:has(ul)").addClass("parent")
like image 4
bryanbcook Avatar answered Nov 04 '22 19:11

bryanbcook