Html multi select option, how to mark selected options?

I use the usual select and mutliselect flags on my site. should <option selected="selected"> use <option selected="selected"> or just <option selected> for selected items?

+8
html css drop-down-menu
source share
2 answers

HTML5 Specification :

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

The selected attribute is a logical attribute.

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

The presence of a logical attribute for an element represents a true value, and the absence of an attribute represents a false value.

If an attribute is present, its value must be either an empty string or a value that does not match the ASCII value for the canonical attribute name, without a leading or trailing space.

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" /> 

Missing an attribute is the only valid syntax for false :

 <option /> 

Recommendation

If you want to write valid XHTML, use selected="selected" since <option selected> not valid XHTML (but valid HTML) and other alternatives are less readable. Otherwise, just use <option selected> as it is shorter.

+11
source share

The correct method according to the W3C will be selected = "selected", as noted here http://www.w3.org/TR/html-markup/option.html#option

Quentin: not duplicated, because this question is related to choice, option, meanwhile your answer is related to checkboxes

(The same answer, a different order to avoid the comments "not an answer, this is a comment" for those who cannot read the full post

+3
source share

All Articles