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)">
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.
If you wanted to submit a default value for the checkbox when it is unchecked, you could include an <input type="hidden"> inside the form with the same name and value , generated by JavaScript perhaps.
It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.
short 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
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