views:

83

answers:

2

Hi there!

I'm in a bit of a dilemma. I need to select div tags based on whether their children have a certain class. This is a code example of the DOM structure:

<div id="container">

    <div id="one">
         <p>This is item one</p>
         <p class="special">This is a description</p>
    </div>

    <div id="two">
         <p>This is item one</p>
         <p>This is a description</p>
    </div>

    <div id="three">
         <p>This is item one</p>
         <p class="special">This is a description</p>
    </div>

</div>

So, what I want to select is a div tag that doesn't have a paragraph with a class of "special", and in the example above, that would be second div tag (#two).

Any ideas?

+14  A: 

You can use the :not and :has selectors:

$('div:not(:has(p.special))')
Greg
+1 for beating me to it.
geowa4
nice and concise - let jQuery do the legwork for you ;p
Rowan
Great, this worked like a charm. Thanks!
Indyber
A: 

Try this:

$("div").filter(function() {
    return $(this).children("p.special").length == 0;
})
Gumbo