How can I uncheck the box in jQuery 1.7?

I am updating from jQuery 1.5.1. I read about the "new" way to "check" checkbox (in 1.6) using

prop("checked", true); 

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

Both of these methods work.

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

or

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

Is there a difference between these methods? What should i use?

thanks

+6
source share
4 answers

According to http://api.jquery.com/removeprop/ .removeProp should not be used to remove the marked. (because it is completely removed and cannot be added back.)

The .removeProp () method removes the 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 delete a property. jQuery first sets the property to undefined and ignores any error that the browser creates. In general, it is only necessary to remove custom properties that have been set on the object, rather than inline (native) properties.

Note. Do not use this method to remove your own properties, such as checked, disabled, or selected. This will delete the property completely and, after removal, cannot be added again to the element. use .prop () to set these properties to false instead.

+12
source

To more accurately answer your question:

I always prefer

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

above

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

since in this case the important difference between an attribute and a property is that deleting the attribute is equal to setting the (is-checked) property to false.

Removing the β€œchecked” property of a flag does not make any sense, because the flag will always be either checked or not checked. Therefore, setting the property to false to uncheck the box is logically consistent, the property is not deleted.

+11
source

hiya I, although from my comment above there will be too much text clogged to write here for clarity: (And I agree with @Claudio)

If this does not help, let me know, I will delete my greetings! :)

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

[quote] from 1.6 i reckon ...

Element

Value is something other than its property or cost attributes. If you want to clear the check box, you want to remove it 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 checkbox with jQuery? I hope this helps, you are right, I think! Hurrah!

+3
source
 $('#someSelector').removeAttr('checked'); 

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

Read more about prop vs attr here. They also mention the verified attribute and call it specifically as an attribute, not a property.

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

-1
source

Source: https://habr.com/ru/post/912080/


All Articles