Phillip Senn
Phillip Senn

Reputation: 47605

jQuery NOT selector

I have the following:

$('input[name=myCheck]').change(function() {
   $('input:checkbox').removeAttr('checked');
});

How do I say: removeAttr('checked') for everybody except this?

Upvotes: 2

Views: 2173

Answers (2)

Nick Craver
Nick Craver

Reputation: 630389

You can use the .not() function to filter out the element(s) you don't want. In this case, use this (which refers to the changed DOM <input> element in the handler), like this:

$('input[name=myCheck]').change(function() {
   $('input:checkbox').not(this).removeAttr('checked');
});

As a side note, .attr('checked', false) also works...or since you only want one checked, consider using a set of radio buttons instead, with no JavaScript at all.

Upvotes: 10

Brad Christie
Brad Christie

Reputation: 101604

Everything except for what, checkboxes or inputs named myCheck?

Either way, it's either in-line using:

:not(<selector>)

or you can use

$('<global selector>').not('<exclusion selector>')

Made this an answer instead of a comment

Upvotes: 1

Related Questions