6

I'm upgrading from jQuery 1.5.1 -- I've read about the "new" way to "check" checkboxes (in 1.6) using

prop("checked", true);

But what is the correct/preferred way to remove a checkbox?

Both these methods appear to work

$('#someSelector').removeProp("checked");

or

$('#someSelector').prop("checked", false);

Is there a distinction between these methods? Which should I be using?

Thanks

rsturim
  • 6,756
  • 15
  • 47
  • 59

4 Answers4

12

According to http://api.jquery.com/removeprop/ .removeProp should not be used to remove checked. (because it is totally removed and can't be added back again.)

The .removeProp() method removes properties set by the .prop() method.

With some built-in properties of a DOM element or window object, browsers may generate an error if an attempt is made to remove the property. jQuery first assigns the value undefined to the property and ignores any error the browser generates. In general, it is only necessary to remove custom properties that have been set on an object, and not built-in (native) properties.

Note: Do not use this method 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.

davepreston
  • 1,260
  • 1
  • 10
  • 20
11

To answer your question more precisely:

I would always prefer

$('#someSelector').prop('checked', false);

over

$('#someSelector').removeProp('checked');

because an important difference between attribute and property is in this case, that removing the attribute equals to setting the property (is-checked) to false.

Removing the "checked" property of a checkbox does not make any sense at all, because the checkbox will always be either checked or unchecked. Therefore setting the property to false to uncheck the box is logically consistent, removing the property is not.

Niko
  • 26,516
  • 9
  • 93
  • 110
3

hiya I though out my comment above will be too much text crammed in so writing it here for clarity: (And I agree with @Claudio)

If this does not help let me know I will remove my post cheers! :)

so from here: http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/

[quote] as of 1.6 i reckon...

element Value is something else then it’s property or attribute’s value. When you want to uncheck a checkbox, you want to remove the checked property so use $(“#subscribe:checked”).prop(“checked”, false);

[quote]

jQuery 1.6+

Use the new .prop() function:

$(".myCheckbox").prop("checked", true);

$(".myCheckbox").prop("checked", false);

Setting "checked" for a checkbox with jQuery? hope it helps, you are correct I reckon! cheers!

Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
-1
$('#someSelector').removeAttr('checked');

Remove the checked attribute and you should be good to go.

More info about prop vs attr here. They also mention the checked attribute and call it out specifically as an attribute, not a property.

http://api.jquery.com/prop/

ctorx
  • 6,841
  • 8
  • 39
  • 53
  • That does not answer the question... why is your answer better than `.removeProp("checked")`? And why is it any better than the alternative `.prop("checked", false)` or `.attr("checked", false)`? – Sparky Mar 30 '12 at 20:49
  • 1
    "attributes" are a subset of "properties", therefore always using `prop` in place of `attr` is perfectly acceptable, and you never have to figure out if a `property` is an `attribute`. – Sparky Mar 30 '12 at 20:51
  • 1
    The jQuery docs you're referring to actually says that it is preferable to use prop() in this case. "If using jQuery 1.6, the code if ( $(elem).attr("checked") ) will retrieve the actual content attribute, which does not change as the checkbox is checked and unchecked. It is meant only to store the default or initial value of the checked property." – Niko Mar 30 '12 at 20:52
  • Worked for me. prop() does not remove the tick but only disables them. – Michael Winther Aug 14 '15 at 09:43