Sounds like a bit of a silly question, but I am wondering what is the best way of stating that a checkbox is checked/unchecked in HTML.
I have seen many different examples:
<input type="checkbox" checked="checked" /> <input type="checkbox" /> <input type="checkbox" checked="yes" /> <input type="checkbox" checked="no" /> <input type="checkbox" checked="true" /> <input type="checkbox" checked="false" />
Which browsers work with which ones of these, and most importantly, does jQuery figure out which box is checked in all 3?
Edit: The W3C spec seems to imply that just the checked attr being there is correct. Does that mean that checked="false" and checked="no" will still check the box though?
The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
A Boolean attribute is an attribute that can only be true or false. How does a Boolean attribute work? According to the HTML specification: The presence of a boolean attribute on an element represents the “true” value, and the absence of the attribute represents the “false” value.
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
In HTML:
<input type="checkbox" checked> <input type="checkbox" checked="checked">
For XHTML you have to use attribute/value matching pairs:
<input type="checkbox" checked="checked" />
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