I want to hide an element if its parent does not have a certain class:
<li class="current_page_parent"> <a href="parent.html">Parent</a> <ul class="children"> <li>foo</li> <li>bar</li> </ul> </li>
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">
.
jQuery("ul.children:not(:parent.current_page_ancestor)").hide();
with no luck.
To select element that does not have specific class with JavaScript, we can use the document. querySelector method with the :not pseudoclass. const li = document. querySelector("li:not(.
Use the parentElement property to select the parent of the element. Use the classList. contains() method to check if the parent contains the class. The method returns true if the element's class list contains the class and false otherwise.
The best way I think:
$(".children").not(".current_page_parent .children").hide();
JSFiddle
Try this:
jQuery("li:not(.current_page_parent) ul.children").hide();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With