David Nathan David Nathan - 3 years ago 107
HTML Question

Get all parents that don't have a child with a certain class

I want to select all div's of parents using jQuery, that don't have children with the class

locked


Example code:

<div class='myparent'>
<div class='mychild locked'></div>
</div>
<div class='myparent'>
<div class='mychild locked'></div>
</div>
<div class='myparent'>
<div class='mychild'></div>
</div>


I feel like I'm really close:

$('div.myparent:not(:has(div[class=locked]))')


But that does not work.

Answer Source

You can just use class selector, there is no need for attribute selector DEMO

$('.myparent:not(:has(div.locked))')

Note:- you can do like this too:- $('.myparent:not(:has(.locked))')

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download