Back to the lesson

Add an option to select

importance: 5

There’s a <select>:

<select id="genres">
  <option value="rock">Rock</option>
  <option value="blues" selected>Blues</option>
</select>

Use JavaScript to:

  1. Show the value and the text of the selected option.
  2. Add an option: <option value="classic">Classic</option>.
  3. Make it selected.

The solution, step by step:

<select id="genres">
  <option value="rock">Rock</option>
  <option value="blues" selected>Blues</option>
</select>

<script>
  // 1)
  let selectedOption = genres.options[select.selectedIndex];
  alert( selectedOption.value );

  // 2)
  let newOption = new Option("classic", "Classic");
  select.append(newOption);

  // 3)
  newOption.selected = true;
</script>