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 ex
A quick test reveals that jQuery equates any value set to being checked.
alert( $('input:checked').length );
// Returns 5
Even an empty string and the negative values.
HTML5 spec:
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
Conclusion:
Let boolean
be a boolean attribute of tag tag
, like checked
and <input type="checkbox"
.
The following are valid, equivalent and true:
<tag boolean />
<tag boolean="" />
<tag boolean="boolean" />
<tag boolean="BoOlEaN" />
The following are invalid:
<tag boolean="0" />
<tag boolean="1" />
<tag boolean="false" />
<tag boolean="true" />
The absence of the attribute is the only valid syntax for false:
<tag />
Recommendation
If you care about writing valid XHTML, use boolean="boolean"
, since <tag boolean>
is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use <tag boolean>
as it is shorter.
According to W3C recommendations your first suggestion is correct.