Add an option to select
önem: 5
There’s a <select>
:
<select id="genres">
<option value="rock">Rock</option>
<option value="blues" selected>Blues</option>
</select>
Use JavaScript to:
- Show the value and the text of the selected option.
- Add an option:
<option value="classic">Classic</option>
. - 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[genres.selectedIndex];
alert( selectedOption.value );
// 2)
let newOption = new Option("Classic", "classic");
genres.append(newOption);
// 3)
newOption.selected = true;
</script>