What is the proper way to check and uncheck a checkbox in HTML5?
HtmlCheckboxCheckedHtml Problem Overview
Looked at the HTML spec, but couldn't make heads or tails of it: http://www.w3.org/TR/html5/the-input-element.html#attr-input-checked
What is the correct way to check a checkbox in HTML (not dynamically)?
checked="true"
checked="checked"
What is the correct way to uncheck a checkbox?
<input type="checkbox" /> with no checked attribute
checked="false"
checked="none"
Where to check the HTML specification to check/uncheck a checkbox?
Html Solutions
Solution 1 - Html
For checked state
Older browsers may need:
<input type="checkbox" checked="checked" />
But nowadays simply do:
<input type="checkbox" checked />
For unchecked state
Remove checked attribute, like:
<input type="checkbox" />
>Reference: http://www.w3.org/TR/html-markup/input.checkbox.html#input.checkbox.attrs.checked
Solution 2 - Html
According to HTML5 drafts, the checked
attribute is a “boolean attribute”, and “The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.” It is the name of the attribute that matters, and suffices. Thus, to make a checkbox initially checked, you use
<input type=checkbox checked>
By default, in the absence of the checked
attribute, a checkbox is initially unchecked:
<input type=checkbox>
Keeping things this way keeps them simple, but if you need to conform to XML syntax (i.e. to use HTML5 in XHTML linearization), you cannot use an attribute name alone. Then the allowed (as per HTML5 drafts) values are the empty string and the string checked
, case insensitively. Example:
<input type="checkbox" checked="checked" />
Solution 3 - Html
<input type="checkbox" checked />
HTML5 does not require attributes to have values
Solution 4 - Html
In jQuery:
To check the checkbox:
$("#checkboxid").attr("checked","checked");
To uncheck the checkbox:
$("#checkboxid").removeAttr("checked");
The other answers hint at the solution and point you to documentation that after further digging will get you to this answer. Jukka K. Korpela has the reason this is the correct answer, basically I followed his link and then looked up the jQuery docs to get to that result. Just figured I'd save future people who find this article those extra steps.
Solution 5 - Html
Complementary answer to Robert's answer http://jsfiddle.net/ak9Sb/ in jQuery
When getting/setting checkbox state, one may encounter these phenomenons:
.trigger("click");
Does check an unchecked checkbox, but do not add the checked attribute. If you use triggers, do not try to get the state with "checked" attribute.
.attr("checked", "");
Does not uncheck the checkbox...
Solution 6 - Html
you can use autocomplete="off" on parent form, so if you reload your page, checkboxes will not be checked automatically
Solution 7 - Html
I'm not entirely sure why this hasn't been mentioned before, but for me, the following works:
To set it to checked
:
<input type="checkbox" checked>
To set it to unchecked
:
<input type="checkbox" unchecked>
(I was having the problem that checkboxes remained checked after reloading the page. Using unchecked
solved my problem, so it might be useful to someone else.)
Solution 8 - Html
You can refer to this page at w3schools but basically you could use any of:
<input checked>
<input checked="checked">
<input checked="">
Solution 9 - Html
<form name="myForm" method="post">
<p>Activity</p>
skiing: <input type="checkbox" name="activity" value="skiing" checked="yes" /><br />
skating: <input type="checkbox" name="activity" value="skating" /><br />
running: <input type="checkbox" name="activity" value="running" /><br />
hiking: <input type="checkbox" name="activity" value="hiking" checked="yes" />
</form>