cdmckay
cdmckay

Reputation: 32290

Select all elements that are not descendents of elements with a certain class

I'm trying to come up with a selector that will select all textarea elements, except those that are descendents of the .noSpell class.

So I want this to match:

<div>
   <textarea />
</div>

but not this

<div class="noSpell">
   <div>
      <textarea />
   </div>
</div>

I tried this:

$(":not(.noSpell) textarea")

but it didn't work, presumably because while it won't match the outer element, it can match any of the inner ones.

So, how would I write a selector that excludes parts of the DOM tree based on a class name?

Upvotes: 2

Views: 154

Answers (3)

Arindam
Arindam

Reputation: 998

@cdmckay Your method is also right.

Try creating textarea as:

<textarea></textarea> 

and see if your code works.

You might want to do this to see if your code works

if($(":not(.noSpell) textarea").length > 0) {
    alert("there");
}
else {
    alert("not there");
}

Cheers!

Upvotes: -1

jfriend00
jfriend00

Reputation: 708036

How about this:

$("textarea").not(".noSpell textarea")

All textareas, then remove the ones that have a parent of .noSpell.

Upvotes: 4

Chris
Chris

Reputation: 58312

The following works for me:

$("textarea").not(".noSpell");

Tested in firefox/firebug.

Upvotes: -1

Related Questions