Reputation: 164
<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
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
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
Reputation: 82893
Try this:
$("li:has(ul)").each(function(){
$(this).addClass("<YOUR_CLASS_NAME>")
}
)
Upvotes: 1