How can I select an element which does not contain a certain child element?
$(':not(.test:has(.example))').css('color', 'red');
http://jsfiddle.net/9fkz7y1g/
jQuery contains()
:
jQuery.contains(document.documentElement, document.body); // true
jQuery.contains(document.body, document.documentElement); // false
$('.test:not(:has(.example))')
-or-
$('.test').not(':has(.example)')
Possibly
$('.test').filter(function() { return !$(this).children('.example').length; });
This filters out any elements that have any child that matches .example
. If you want to filter based on descendants (not just children) that you can substitute .find
for .children
.