Quantcast
Channel: What's the proper value for a checked attribute of an HTML checkbox? - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Answer by Ciro Santilli 新疆再教育营六四事件法轮功郝海东 for What's the proper value for a checked attribute of an HTML checkbox?

$
0
0

HTML5 spec:

http://www.w3.org/TR/html5/forms.html#attr-input-checked :

The disabled content attribute is a boolean attribute.

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:

The following are valid, equivalent and true:

<input type="checkbox" checked /><input type="checkbox" checked="" /><input type="checkbox" checked="checked" /><input type="checkbox" checked="ChEcKeD" />

The following are invalid:

<input type="checkbox" checked="0" /><input type="checkbox" checked="1" /><input type="checkbox" checked="false" /><input type="checkbox" checked="true" />

The absence of the attribute is the only valid syntax for false:

<input />

Recommendation

If you care about writing valid XHTML, use checked="checked", since <input checked> is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use <input checked> as it is shorter.


Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>