Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for HTML <option> : selected VS selected="selected"

What is the best practice to pre-select an <option> in a <select>?

According to different websites, both works. But which one is better? the most compatible?

<option selected="selected">Foo</option>
<option selected>Bar</option>
like image 254
4wk_ Avatar asked Aug 03 '15 08:08

4wk_


Video Answer


2 Answers

If you are writing XHTML, selected="selected" is required.

If you are writing HTML, selected is fewer keystrokes and fewer bytes.

Neither has better compatibility with browsers.

like image 72
Quentin Avatar answered Sep 22 '22 04:09

Quentin


According to the HTML4 standard, using selected implies it is selected="selected". So the standard already defines both are supported. Their code sample already includes a code block just using selected.

For compatibility with XHTML (specifically XML is the 'problem' here), there is a selected="selected" option.

HTML5 also defines selected on its own is valid, so no need to worry.

like image 20
Patrick Hofman Avatar answered Sep 20 '22 04:09

Patrick Hofman