frenchie
frenchie

Reputation: 51937

jquery unselecting all checkboxes

I have this HTML:

<div id="SomeID">

  <input type="checkbox" class="SomeClass">
  <input type="checkbox" class="SomeClass">
  <input type="checkbox" class="SomeClass">

</div>

I want to unselect all the checkboxes that are of type SomeClass.

  $('#SomeID .SomeClass').each(function () {
     $(this)....
  });

How do I do that?

Thanks.

Upvotes: 1

Views: 725

Answers (2)

zzzzBov
zzzzBov

Reputation: 179046

You'd use prop:

$('#SomeID .SomeClass').prop('checked', false);

If you're in a version of jQuery pre 1.6, you'd use:

$('#SomeID .SomeClass').each(function () {
    this.checked = false;
});

Don't use attr or removeAttr for changing :checked state!

There is a difference between changing the checked property on the element and changing the checked attribute, it's subtle, but worth mentioning.

If you change the attribute value, and the user resets the form, the "reset" state will be pulled from whether the [checked] attribute exists. If you've been adding/removing the [checked] attribute, you will have lost the original state of the checkbox.

Here's an example

Upvotes: 10

Chad Ferguson
Chad Ferguson

Reputation: 3091

$(".SomeClass").prop('checked', false);

Upvotes: 0

Related Questions