An input of type checkbox when fires its onchange
event the this.value
is always set to on
even when the checkbox is ticked off in which case I'd expect it to be off
Is this the intended behavior?
<input type="checkbox" onchange="alert(this.value)">
An input of type checkbox when fires its onchange
event the this.value
is always set to on
even when the checkbox is ticked off in which case I'd expect it to be off
Is this the intended behavior?
<input type="checkbox" onchange="alert(this.value)">
Share
Improve this question
asked Apr 29, 2015 at 1:09
laggingreflexlaggingreflex
34.6k36 gold badges143 silver badges200 bronze badges
1 Answer
Reset to default 20short answer: do not use value
to check checked status on checkboxes, use checked
instead
<input type="checkbox" onchange="alert(this.checked)">
and in case that you wonder why is this always-"on" value, there's a specification for that in HTML5 specification:
default/on
On getting, if the element has a value attribute, it must return that attribute's value; otherwise, it must return the string "on". On setting, it must set the element's value attribute to the new value.
http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on