Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

selected — default option selection

The boolean selected attribute specifies an option element's default selection. It is not a reliable reading of the current choice: in JavaScript, selected describes the current state, while defaultSelected reflects the attribute.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

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

selected is a boolean attribute on an option element that specifies its default selection. Put it on option, not on select. Without multiple, a select must not contain more than one option with this attribute; with multiple, several default selections are allowed.

The forms selected, selected="", and selected="selected" are equivalent. Writing selected="false" is invalid and still means the attribute is present. To remove the default selection, omit or remove the attribute.

In JavaScript, option.defaultSelected reflects the attribute, whereas option.selected reports the current selection. A user selection change or assignment to option.selected sets an internal dirtiness flag; afterward, changing the attribute no longer directly controls that option's selection. Use option.selected or select.value to change the current choice.

form.reset() restores selections from the current selected attributes, clears dirtiness flags, and applies the select element's selection rules. An absent attribute does not guarantee an unselected current state: an ordinary drop-down can automatically select its first enabled option.

Examples

<!doctype html>
<meta charset="utf-8">
<title>selected</title>
<form id="demo">
  <label for="color">Color</label>
  <select id="color" name="color">
    <option value="red">Red</option>
    <option value="blue" selected>Blue</option>
  </select>
</form>
<pre id="output"></pre>
<script>
  const form = document.getElementById('demo');
  const select = document.getElementById('color');
  const blue = select.options[1];
  const lines = [];
  function record() {
    lines.push(`${select.value} ${blue.selected} ${blue.defaultSelected}`);
  }
  record();
  select.value = 'red';
  record();
  form.reset();
  record();
  document.getElementById('output').textContent = lines.join('\n');
</script>

Result

blue true true
red false true
blue true true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources