user555600
user555600

Reputation: 164

how to find <ul> within <li> then add class to <li>

<ul>
<li>nav</li>
<li>nav</li>
<li>nav</li>
<li class="how do i addClass here">nav "how to addClass with in this li"
 <ul>
     <li>nav</li>
     <li>nav</li>
     <li class="how do i addClass here">nav "how to addClass with in this li"
          <ul>
             <li>nav</li>
             <li>nav</li>
             <li>nav</li>
         </ul>
     </ul>
 </li>
 </ul>

Upvotes: 2

Views: 3352

Answers (3)

Mike
Mike

Reputation: 643

$('body ul:nth-child(1) li:nth-child(4)').addClass('test').find('ul li:nth-child(3)').addClass('test2');

live demo: http://jsfiddle.net/ps8AA/

Upvotes: 0

user113716
user113716

Reputation: 322452

$('ul').parent('li').addClass('someclass');

Select all the <ul> elements, then do .parent('li'). This will only select the immediate parent element if it is a <li> element. Then use .addClass() to add the class.

This uses a valid CSS selector, which is a good thing for performance in browsers that support querySelectorAll.

Browsers that don't support it should perform fast as well, since jQuery likely does a getElementsByTagName.

Upvotes: 3

Chandu
Chandu

Reputation: 82893

Try this:

$("li:has(ul)").each(function(){
   $(this).addClass("<YOUR_CLASS_NAME>")
 }
)

Upvotes: 1

Related Questions