HTML Checked, Disabled and Selected attributes

Checked, Disabled and Selected are common HTML Boolean attributes. So what’s the correct way to specify these (and any other similar Boolean attributes) in your HTML markup?


From the HTML 5 specs we get the following.

DO THIS

To add the attributes, you can do this:

<input type="checkbox" checked="checked">
<input disabled="disabled">
<option selected="selected">

Or use an empt string:

<input type="checkbox" checked="">
<input disabled="">
<option selected="">

/Edit 19 July 2011/ Attributes without a value are also valid:

<input type="checkbox" checked>
<input disabled>
<option selected>

/Edit 19 July 2011/ And the double quotes are optional:

<input type=checkbox checked=checked>
<input disabled=disabled>
<option selected=selected>

To not apply the attribute, just leave them off:

<input type="checkbox">
<input>
<option>

DON’T DO THIS

/Edit 19 July 2011/

Leaving the attributes without a value is invalid:

<input type="checkbox" checked>
<input disabled>
<option selected>

Using true/false values is invalid:

<input type="checkbox" checked="true">
<input disabled="false">
<option selected="true">
This entry was posted in Web Development and tagged , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

6 Comments

  1. Posted July 20, 2010 at 8:44 am | Permalink

    Which is a perfect example of why the standards committee is a bunch of idiots. Setting the values of boolean attributes to true/false makes perfect sense, yet it’s not valid.

  2. JohnEric
    Posted July 20, 2010 at 1:28 pm | Permalink

    I agree with Peter. In what version of reality does using checked=”" make sense that a checkbox is selected and checked=”true” doesn’t.

  3. Posted July 20, 2010 at 10:38 pm | Permalink

    Yeah, unfortunate it worked out this way, but from what I understand it was done to maximise backward compatibility with older browsers.

    And just to add to this mix of Boolean attributes, it looks like the new HTML5 spellcheck attribute will require either a “true” or “false” value (or empty string which also means true). Using spellcheck=”spellcheck” will be invalid.

  4. David Phillips
    Posted July 18, 2011 at 8:29 pm | Permalink

    This post seems to be incorrect.

    According to the ‘boolean attributes’ link provided in the post, the form in which the value is omitted altogether is perfectly fine. The post claims that it is invalid.

    Admittedly the language in the spec can be read as ruling this form out but the examples provided make it perfectly clear that this form is acceptable.

  5. Posted July 18, 2011 at 10:48 pm | Permalink

    David, thanks very much for the fix. I’ve wrangled the entry into something that should be correct now. Some good examples have since been added to the spec that make this clearer now; something I missed on my first reading. Doh!

  6. Posted July 25, 2011 at 9:37 pm | Permalink

    I have to disagree with the sentiment that standards committees got this one wrong. Yes, if you are an XML fanatic than the idea of thinking of a Boolean value as being a flag that is present or not present is somewhat disconcerting, but I actually think this is one place where XML gets it wrong. Why shouldn’t you just check for the presence of an attribute as a Boolean? Setting an attribute to equal “true” or “false” in information bearing data-structures is overly verbose and unnecessary, setting equivalence in a data structure should only be necessary if there are more than two values, or there is the potential for there to be more than two values, otherwise you’re wasting space.

    In order to vindicate my claim I would like to point out that this is what noSQL solutions, like MongoDB, now do in order to be horizontally scalable (i.e. there are no columns). By the way, setting checked=”true”, will check your check box (you could write “poop” as it’s value and it would work), it’s simply not semantic, either from the perspective of XML or HTML. I realize I’m splitting hairs right now, but it really irritates me when people make a bold declarative like, “In what reality…”: apparently in your reality.