views:

291

answers:

4

I'm looking for a way to select all elements on a page, except those with a specified DOM location.. Here's an example of what I'd like to do:

jQuery('*').except('.ignore').bind('click', function(e) { ... });

Is this possible in a "native jQuery" way?

+10  A: 

You want to use the :not() selector:

jQuery(":not(.ignore)").bind("click", function(e) { ... });
Andrew Hare
Ah, I guess the asterisk in my example is probably redundant.
Matt Ball
Yes it is - this example works with or without the asterisk.
Andrew Hare
Now looking at it, I think I prefer without the asterisk.
Andrew Hare
Thanks guys! :D Much appreciated.
Lorren Biffin
+7  A: 

jQuery not-selector to the rescue!

$('*:not(.ignore)').bind('click', function(e) { ... });
Matt Ball
+1  A: 

Another way, if you already have selectors for both:

$('.foo').not('.ignore').bind(...);

Also, more filters.

orip
A: 

On the other hand, doing something to every element on a page simultaneous is nasty. There's a better way. I would recommend binding to the body then ignoring clicks on some elements:

$(document.body).click(function(e){
    if($target.closest('.ignore').length) return true;
    ...
});

…Or using jQuery 1.3's .live(), which does this for you:

 $(":not(.ignore)").live(function(e){
    ...
});
Sidnicious