Alex
Alex

Reputation: 68470

jQuery - select elements that are not within a ID

How can I select all elements that have a title attribute, but are not contained by a element with the #boo ID ?

like $("*[title]").each()...

but not elements which are in #boo :)

Upvotes: 4

Views: 2844

Answers (6)

Brian
Brian

Reputation: 4984

I've had to do this before and used something like:

$(":not(#boo) > [title]")

Upvotes: 1

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

$("[title]").not("#boo [title]").each()

demo

and as a side note, when you use id in getting the element, it is faster if you don't prefix the element's tag. For example, just use #boo instead of div#boo. demo <-- try to look at the console of firebug for time comparison.

Upvotes: 9

Phrogz
Phrogz

Reputation: 303224

There are may ways to skin this cat. Here are two more:

$('[title]:not(#boo *[title])');
$('[title]').not('#boo *[title]');

Upvotes: 2

sevenseacat
sevenseacat

Reputation: 25029

$('*:not(#boo) *[title]'); should work.

Upvotes: 6

Chris Hasiński
Chris Hasiński

Reputation: 3085

Use filter()

http://api.jquery.com/filter/

Upvotes: 2

qwertymk
qwertymk

Reputation: 35276

$('[title]').filter(function(){return $(this).parents('#boo').length === 0;})

Upvotes: 3

Related Questions