Selecting all divs except one

<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> ?

Answers


$('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";
});

Live DEMO


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))')

$('div.b').filter(function () {
    return $(this).find('p:contains(not me)').length == 0;
})

jsFiddle Demo


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> 

This will give you two div who have class="b" and does not have p (paragraph)

Live Demo

$('div.b:not(:has(p))')

And here's another one

$("div.b:contains('not me')")

edit: sorry, here's with not

$("div.b:not(:contains('not me'))")

Need Your Help

supressLint cannot be resolved to a type error (android beginner tutorial online)

android lint

In thefollowing Code, I get a problem when following the Android beginners tutorial at http://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity

How to transform a nested C++11 bind expression

c++ c++11 metaprogramming bind compile-time

Is it possible to transform a nested C++11 bind expression? For example, in the code below, the bind expression associated with f will first multiply its argument by two, before adding one to the r...