19

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?

mO_odRing
  • 1,935
  • 3
  • 15
  • 19

4 Answers4

30

You want to use the :not() selector:

jQuery(":not(.ignore)").bind("click", function(e) { ... });
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
24

Another way, if you already have selectors for both:

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

Also, more filters.

orip
  • 73,323
  • 21
  • 116
  • 148
8

jQuery not-selector to the rescue!

$('*:not(.ignore)').bind('click', function(e) { ... });
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

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){
    ...
});
s4y
  • 50,525
  • 12
  • 70
  • 98