Naoise Golden Naoise Golden -3 years ago 116
jQuery Question

How to select an element which parent is not specified class?

I want to hide an element if its parent does not have a certain class:

HTML



<li class="current_page_parent">
<a href="parent.html">Parent</a>
<ul class="children">
<li>foo</li>
<li>bar</li>
</ul>
</li>


jQuery



jQuery("ul.children").hide();


Currently always hides
<ul class="children">
regardless of class. I would like to close it only if parent is
:not
an
<li class="current_page_parent">
.

I've tried:



jQuery("ul.children:not(:parent.current_page_ancestor)").hide();


with no luck.

Answer Source

Try this:

jQuery("li:not(.current_page_parent) ul.children").hide();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download