In html5 to set a checkbox as checked, should I simply use checked (as a property) or checked="checked" ( as an attribute)?
It is an attribute in either case. And it sets a value (the same value, true
) on a DOM property of the element node in either case.
For most purposes, it does not matter which syntax you use. However, there are some points to note:
- If you use HTML5 in XML serialization (“XHTML5”), you must use
checked="checked"
. - In styling, the syntaxes are not quite equivalent when using attribute selectors (the shorter form does not match
[checked=checked]
), but this does not matter in practice:[checked]
matches checked checkboxes in either case. - The clumsy syntax
checked="checked"
is a holdover from SGML and included for compatibility only, so it may make your code look old-fashioned (which rarely matters).
<!-- Default to unchecked -->
<input type="checkbox">
<!-- Default to checked, XHTML -->
<input type="checkbox" checked="checked" />
<!-- Default to checked, HTML5 -->
<input type="checkbox" checked>
Source: http://css-tricks.com/indeterminate-checkboxes/
Checked
is a boolean attribute in HTML 5. A true value is indicated by the attribute being present, and a false value is indicated by its absence. If it is present, its value should either be empty or set to the property name checked="checked"
. Either of these forms are correct:
<input type="checkbox" checked="checked" />
<input type="checkbox" checked>
https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes