How to mark selected options in for the option HTML tag?

css, drop-down-menu, html

Solution

HTML5 spec:

https://www.w3.org/TR/html5/forms.html#attr-option-selected

The selected 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:

<option selected />
<option selected="" />
<option selected="selected" />
<option selected="SeLeCtEd" />

The following are invalid:

<option selected="0" />
<option selected="1" />
<option selected="false" />
<option selected="true" />

The absence of the attribute is the only valid syntax for false:

<option />

Recommendation

If you care about writing valid XHTML, use `selected="selected"`, since `<option selected>` is invalid XHTML (but valid HTML) and other alternatives are less readable. Else, just use `<option selected>` as it is shorter.

Problem

I am using normal select and mutliselect boxes on my site. should I use `<option selected="selected">` or simply `<option selected>` for selected items ?

Original source

Related problems