Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should setting `checkbox.checked = false` not clear the HTML attribute too?

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?

like image 503
Kev Avatar asked Dec 04 '09 21:12

Kev


People also ask

Does checkbox have value attribute?

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.

Does checkbox return true or false?

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.


1 Answers

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.)

like image 186
bobince Avatar answered Oct 05 '22 22:10

bobince