What is the proper way to check and uncheck a checkbox in HTML5?
Asked Answered
G

9

84

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?

Granulocyte answered 3/10, 2012 at 1:5 Comment(0)
B
104

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

Biondo answered 3/10, 2012 at 1:10 Comment(5)
Or <input type="checkbox" checked="" /> is checked as well.Granulocyte
While, both are correct, my personal observation has been that checked="checked" accounts for mouse-event based check and uncheck, there as checked is programming based.Yarbrough
The link got broken, would you mind updating it? A citation inside your answer would be nice so that if the link gets broken again, those who read can see the explanation without further searchCandracandy
At least we can back up this answer with w3schools.com/tags/att_input_checked.aspCandracandy
@ B Seven It's worthy an upvoteGallman
S
28

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" />
Salmi answered 3/10, 2012 at 8:42 Comment(0)
P
8

<input type="checkbox" checked />

HTML5 does not require attributes to have values

Poston answered 3/10, 2012 at 1:7 Comment(1)
Actually, this is not an answer, they asked "What is the correct way to uncheck a checkbox?"Candracandy
U
6

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.

Unswerving answered 30/1, 2014 at 18:7 Comment(2)
You should use .prop(), not .attr()Cerallua
.attr() is not working and setAttribute complains after you remove the attribute by removeAttr. what worked for me was: w3schools.com/jsref/prop_checkbox_checked.aspMarauding
D
6

you can use autocomplete="off" on parent form, so if you reload your page, checkboxes will not be checked automatically

Danita answered 6/3, 2019 at 5:56 Comment(0)
S
5

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...

Swob answered 2/4, 2014 at 9:8 Comment(0)
K
5

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.)

Klecka answered 20/3, 2021 at 15:33 Comment(2)
Worked! if (in_array($row["ID"], $array)) $m = 'checked'; else $m = 'unchecked'; }Prefect
There is no such attribute as unchecked - if you're going to add invalid markup to your code you might as well call it dog-biscuits: it'll have the same effect as the absence of a checked attribute.Gregg
E
4

You can refer to this page at w3schools but basically you could use any of:

<input checked>
<input checked="checked">
<input checked="">
Elkeelkhound answered 3/10, 2012 at 1:10 Comment(2)
Yes they are, try and you'll seeElkeelkhound
Actually, this is not an answer, they asked "What is the correct way to uncheck a checkbox?"Candracandy
R
1
<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>
Repetitive answered 3/10, 2012 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.