eric2872
eric2872

Reputation: 122

jQuery selection exclude certain elements

Ok so I have this code but was wondering how would I exclude certain divs like for example div id="foo"

Like this

$(document (except #foo) ).click(function() {
$("#re").animate({
  "margin-top": "0px"
}, 800);
$("#r").animate({
  "margin-top": "0px"
}, 800);
});

Upvotes: 0

Views: 33

Answers (2)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26163

Use the :not pseudo-selector...

$("div:not(#foo)");

That will select all divs, except the one with an ID of foo.

Upvotes: 1

Ram
Ram

Reputation: 144739

You can check the target of event object:

$(document).on('click', function(event) {
   if ( $(event.target).closest('#foo').length ) return;
   // ... 
});

Upvotes: 1

Related Questions