BenMorel
BenMorel

Reputation: 36524

Does HTML5 explicitly allow a <select> without selected <option>?

I've checked with the validator, and it looks like the following code is acceptable:

<select>
    <option>I don't have the 'selected' attribute</option>
</select>

Furthermore, all browsers seem to consider the first <option> as if it was selected.

But is this behaviour explicitly allowed, and documented, in the spec?

I've found a very old discussion that pointed to the HTML4 spec, saying:

If no OPTION element has the selected attribute set, user agent behavior for choosing which option is initially selected is undefined.

Did this change with HTML5? A pointer to the relevant section of the spec would be appreciated.

Upvotes: 0

Views: 171

Answers (1)

BoltClock
BoltClock

Reputation: 723749

This is now explicitly defined by HTML5 to a certain extent:

If the multiple attribute is absent and the element's display size is 1, then whenever there are no option elements in the select element's list of options that have their selectedness set to true, the user agent must set the selectedness of the first option element in the list of options in tree order that is not disabled, if any, to true.

In your given markup, the multiple and size attributes are absent on the <select>, and the only <option> element inside it is not disabled and does not have a selected attribute explicitly set. For a non-multiple <select> element the default display size is 1, and so the above rule must be applied by a browser. Note that this is consistent with current browser behavior, and with good reason.

It still does not appear to specify what a browser should do in the event that there are no enabled <option> elements in the given <select>, however.

Upvotes: 6

Related Questions