131

With the introduction of the prop method, now I need to know the accepted way of unchecking a checkbox. Is it:

$('input').filter(':checkbox').removeAttr('checked');

or

$('input').filter(':checkbox').prop('checked',false);
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

5 Answers5

149

jQuery 3

As of jQuery 3, removeAttr does not set the corresponding property to false anymore:

Prior to jQuery 3.0, using .removeAttr() on a boolean attribute such as checked, selected, or readonly would also set the corresponding named property to false. This behavior was required for ancient versions of Internet Explorer but is not correct for modern browsers because the attribute represents the initial value and the property represents the current (dynamic) value.

It is almost always a mistake to use .removeAttr( "checked" ) on a DOM element. The only time it might be useful is if the DOM is later going to be serialized back to an HTML string. In all other cases, .prop( "checked", false ) should be used instead.

Changelog

Hence only .prop('checked',false) is correct way when using this version.


Original answer (from 2011):

For attributes which have underlying boolean properties (of which checked is one), removeAttr automatically sets the underlying property to false. (Note that this is among the backwards-compatibility "fixes" added in jQuery 1.6.1).

So, either will work... but the second example you gave (using prop) is the more correct of the two. If your goal is to uncheck the checkbox, you really do want to affect the property, not the attribute, and there's no need to go through removeAttr to do that.

Community
  • 1
  • 1
John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • 37
    @tandu: You *can*, but you shouldn't. From the docs: "**Note:** Do not use [`removeProp()`] to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use `.prop()` to set these properties to false instead." `removeProp` is really intended for use with custom properties only. – John Flatness Oct 11 '11 at 20:51
25

use checked : true, false property of the checkbox.

jQuery:

if($('input[type=checkbox]').is(':checked')) {
    $(this).prop('checked',true);
} else {
    $(this).prop('checked',false);
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
Suraj Rawat
  • 3,685
  • 22
  • 33
15

I recommend to use both, prop and attr because I had problems with Chrome and I solved it using both functions.

if ($(':checkbox').is(':checked')){
    $(':checkbox').prop('checked', true).attr('checked', 'checked');
}
else {
    $(':checkbox').prop('checked', false).removeAttr('checked');
}
Ragnar
  • 4,393
  • 1
  • 27
  • 40
2

Another alternative to do the same thing is to filter on type=checkbox attribute:

$('input[type="checkbox"]').removeAttr('checked');

or

$('input[type="checkbox"]').prop('checked' , false);

From the jQuery API Documentation:

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
tonnoz
  • 454
  • 4
  • 12
  • 3
    The question was "Which one of these is more correct", ?not "Are there any other ways of doing this?". You don't appear to have attempted to answer the question – Andrew Stubbs Jul 24 '14 at 08:55
-4
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>   

  

  <input id="checkbox" type="checkbox" onchange='act(this)'>
     
  <input id="checkbox" type="checkbox" onchange='act(this)'>
  
  <script>
  
  
   function act(element){
   
       if($(element). prop('checked'))  
       {
           console.log('You have Checked it');
       }else
       {
        console.log('You Un-Checked it');
       }
   }
   
   </script>    
  • 3
    This code snippet does not answer the original question of what is the accepted (correct) way of unchecking a checkbox. – seemly Jun 25 '21 at 09:33