Here's my HTML:
<input id="test" type="checkbox" checked="">
Here's a Firebug excerpt:
>>> test
<input id="test" type="checkbox" checked="">
>>> test.checked = false
false
>>> test
<input id="test" type="checkbox" checked="">
Um...am I missing something, or should that last line not read the following?
<input id="test" type="checkbox">
UI-wise, the checkbox does indeed uncheck when I execute the checked = false
line.
Anyway, if there's some legitimate explanation for this, then what's the proper way to uncheck a checkbox from JavaScript, if not checked = false
?
Note: Unlike other input controls, a checkbox's value is only included in the submitted data if the checkbox is currently checked . If it is, then the value of the checkbox's value attribute is reported as the input's value.
The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.
The value
attribute of input type="text"
and the checked
or selected
attributes of input type="checkbox"
, radio
and option
correspond to the initial value of the form field, not the current value the user or script has set. Consequently changing the checked
property does not alter the attribute value, and setting the checked
attribute does not alter the real visible value that's going to be submitted with the form.
The checked="checked"
attribute corresponds to the defaultChecked
DOM property, not the checked
property. Similarly, the value="..."
attribute corresponds to defaultValue
.
(Note there are IE gotchas here due to IE not knowing the difference between a property and an attribute.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With