Reputation: 2915
<div id="main">
<div class="a"></div>
<div class="b"><p>not me</p></div>
<div class="b"></div>
<div class="b"></div>
<div class="c"></div>
</div>
How we can write a selector to select all divs with class b
except whose child is <p>not me</p>
?
Upvotes: 3
Views: 2185
Reputation: 743
And here's another one
$("div.b:contains('not me')")
edit: sorry, here's with not
$("div.b:not(:contains('not me'))")
Upvotes: 0
Reputation: 148110
This will give you two div who have class="b" and does not have p (paragraph)
$('div.b:not(:has(p))')
Upvotes: 1
Reputation: 8184
The best way I usualy do:
$('div.b').filter(function () {
return !$(this).hasClass('notMe');
});
<div id="main">
<div class="a"></div>
<div class="b notMe"><p>not me</p></div>
<div class="b"></div>
<div class="b"></div>
<div class="c"></div>
</div>
Upvotes: 1
Reputation: 150253
$('div.b:not(:has(p))').........
Or the readable version
$('div.b').filter(function(){
return !$(this).find('p').length;
});
If you want to match the content as well:
$('div.b').filter(function(){
return $(this).find('p').text() !== "not me";
});
Upvotes: 10
Reputation: 78671
$('div.b').filter(function () {
return $(this).find('p:contains(not me)').length == 0;
})
Upvotes: 1
Reputation: 34107
demo http://jsfiddle.net/46nC5/1/
Since you are specifically looking for class b
made a demo for you hence sharing.
In this demo you will see the not me getting fade out, rest will stay as it is.
i.e. With :not
+ :has
it can be done
code
$('div.b:has(p)').
or
$('div.b:not(:has(p))')
Upvotes: 3