What values for checked and selected are false?
Asked Answered
C

6

88

I think according to W3 spec, you're supposed to do

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

And

 selected="selected"

But, most browsers will accept it you just write "CHECKED" and don't give it a value. So, what if you do include the attribute, are there any values that would be (consistently) considered false?

Capsaicin answered 19/11, 2010 at 19:30 Comment(2)
@Ms2ger: Whoops, not enough coffee this morning. I was in Boolean mode. :)Tannenwald
possible duplicate of #1034444 because both are boolean attributes (not flagged)Magenmagena
C
169

There are no values that will cause the checkbox to be unchecked. If the checked attribute exists, the checkbox will be checked regardless of what value you set it to.

<input type="checkbox" checked />
<input type="checkbox" checked="" />
<input type="checkbox" checked="checked" />
<input type="checkbox" checked="unchecked" />
<input type="checkbox" checked="true" />
<input type="checkbox" checked="false" />
<input type="checkbox" checked="on" />
<input type="checkbox" checked="off" />
<input type="checkbox" checked="1" />
<input type="checkbox" checked="0" />
<input type="checkbox" checked="yes" />
<input type="checkbox" checked="no" />
<input type="checkbox" checked="y" />
<input type="checkbox" checked="n" />

Renders everything checked in all modern browsers (FF3.6, Chrome 10, IE8).

Capsaicin answered 6/1, 2011 at 8:53 Comment(2)
Why is this accepted answer ? Isn't the OP question was what value you have to set to actually set it as unchecked ?Smorgasbord
@Smorgasbord Yes, that was my original question. I guess I should have stated it more clearly in my answer: there are no values that will cause it to render unchecked. If the attribute exists, it will be checked.Capsaicin
C
42

The checked and selected attributes are allowed only two values, which are a copy of the attribute name and (from HTML 5 onwards) an empty string. Giving any other value is an error.

If you don't want to set the attribute, then the entire attribute must be omitted.

Note that in HTML 4 you may omit everything except the value. HTML 5 changed this to omit everything except the name (which makes no practical difference).

Thus, the complete (aside from variations in cAsE) set of valid representations of the attribute are:

<input ... checked="checked"> <!-- All versions of HTML / XHTML -->
<input ...          checked > <!-- Only HTML 4.01 and earlier -->
<input ... checked          > <!-- Only HTML 5 and later -->
<input ... checked=""       > <!-- Only HTML 5 and later -->

Documents served as text/html (HTML or XHTML) will be fed through a tag soup parser, and the presence of a checked attribute (with any value) will be treated as "This element should be checked". Thus, while invalid, checked="true", checked="yes", and checked="false" will all trigger the checked state.

I've not had any inclination to find out what error recovery mechanisms are in place for XML parsing mode should a different value be given to the attribute, but I would expect that the legacy of HTML and/or simple error recovery would treat it in the same way: If the attribute is there then the element is checked.

(And all the above applies equally to selected as it does to checked.)

Costume answered 19/11, 2010 at 19:53 Comment(2)
Okay... because I'm writing a CSS selector engine for C#, and I'm trying to find "all the checked checkboxes". Right now I'm just checking for the presence of the attribute, but if there are some false values, then I have to do further checking.Capsaicin
Just tested...seems to be true. Makes my life easier.Capsaicin
M
20

No value is considered false, only the absence of the attribute. There are plenty of invalid values though, and some implementations might consider certain invalid values as false.

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 type="checkbox" />

Recommendation

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

Magenmagena answered 5/7, 2014 at 16:28 Comment(1)
Invalid but work perfectly fine in pretty much every browser that I'm aware of. It's easy to stick to the valid stuff if you're the one writing the HTML, but you have to support the invalid stuff too if you're writing a parser :|Capsaicin
I
4

The empty string is false as a rule.

Apparently the empty string is not respected as empty in all browsers and the presence of the checked attribute is taken to mean checked. So the entire attribute must either be present or omitted.

Irremediable answered 19/11, 2010 at 19:32 Comment(2)
When I tested checked="" in Firefox and IE (both 7 and 8), it still rendered a checked state.Herophilus
@jwiscarson Quite right, it does - my answer was incorrect so I have amended it. The state is determined via the presence of absence of the attribute as opposed to its value.Irremediable
H
4

Actually, the HTML 4.01 spec says that these attributes do not require values. I haven't personally encountered a situation where providing a value rendered these controls as unselected.

Here are the respective links to the spec document for selected and checked.

Edit: Firebug renders the checkbox as checked regardless of any values I put in quotes for the checked attribute (including just typing "checked" with no values whatsoever), and IE 8's Developer Tools forces checked="checked". I'm not sure if any similar tools exist for other browsers that might alter the rendered state of a checkbox, however.

Herophilus answered 19/11, 2010 at 19:33 Comment(5)
Most people and certainly most code validators now expect the use of XHTML, Transitional at least (if not Strict) and in XHTML the values are required. HTML 4.01 is not the current standard.Irremediable
I'm sure you're right in that regard, but I'm not sure that makes my answer incorrect. The XHTML reference I found here (xhtml.com/en/xhtml/reference/input) include no information about values for this attribute that would negate the attribute's core effect.Herophilus
HTML 4.01 is the latest standard for HTML. XHTML is a different, although related, language that still doesn't have support in the latest (final) version of the most commonly used browser on the planet (IE8). (Coping with XHTML served with a claim that it is HTML not withstanding).Costume
The vast majority of professional web developers tend to code for XHTML Transitional, and the use of original HTML 4.01, is unusual. Does IE* ever fully support anything. ;-)Irremediable
There is 'fully' and there is 'not at all'. Say hello to Mr. Download Prompt. (And the vast majority of professional web developers treat Doctypes in a cargo cult fashion without understanding the implications (beyond quirks/standards mode, and then only if we're lucky)).Costume
F
0

checked attribute can be set in the following way in javascript. (isOrdered stores a boolean value)

<input checked={isOrdered} type="checkbox"/> Ordered
Formulaic answered 3/4, 2021 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.