According to this MDN article, the checked
attribute of a checkbox only indicates whether a checkbox is ticked by default, and not its current state.
However, very basic testing reveals it does reflect the checkbox’s current state :
<input type="checkbox" id="test">
<button type="button" onclick="console.log(document.querySelector('#test').checked);">Test</button>
Is the MDN article wrong, or are browsers simply not conforming with the specification here ?
2
Answers
The article clarifies that the
HTMLInputElement
‘schecked
value does update with state, which you can read more about here.With that said, I have no idea how to explain the difference between these concepts, and am not sure I even understand it myself. Any more clarity is welcome!
The MDN article is correct. It discusses the
checked
attribute, whereas your sample code accesses thechecked
property. The following example demonstrates the difference between thechecked
attribute and thechecked
property:The Log Attribute button logs the initial state (where any non-null string means "checked"), and the Log Property button logs the current state.