Luke
Luke

Reputation: 20090

Are there any benefits to setting a value for an HTML boolean attribute?

I know that a boolean attribute can optionally have a value. From the HTML5 spec:

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. Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

I suppose there is a valid historical reason that explains why a value is allowed (and only allow a case-insensitive name match)... but my question is: What are the benefits, if any, of having a value for these boolean attributes? (Or disadvantages, if any.)

For example:

<option selected="selected">...</option>
...
<input required="required" ... />

versus

<option selected>...</option>
...
<input required ... />

Upvotes: 1

Views: 129

Answers (2)

user2939415
user2939415

Reputation: 894

Using an empty string is consistent with having the attribute without a value. If you set a boolean attribute to empty string then the attribute will show up as unset (without a value) in Chrome dev tools.

Upvotes: 0

Solal Pirelli
Solal Pirelli

Reputation: 1219

If you need to do XHTML for whatever reason, you'll have to do that since attributes must have values in XML.

Upvotes: 1

Related Questions