Sometimes, we want to change the selected option of an HTML select element with JavaScript.
In this article, we’ll look at how to change the selected option of an HTML select element with JavaScript.
How to change the selected option of an HTML select element with JavaScript?
To change the selected option of an HTML select element with JavaScript, we set the value
property.
For instance, we write
<select id="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>
to add a select drop down.
Then we write
document.getElementById("state").value = "AZ";
to select thge select element with getElementById
.
And we set its value
property to the value of the value
attribute of the option we want.
Conclusion
To change the selected option of an HTML select element with JavaScript, we set the value
property.