Categories
JavaScript Answers

How to change the selected option of an HTML Select element with JavaScript?

Spread the love

To change the selected option of an HTML Select element with JavaScript, we set the value property.

For instance, we write

<select id="sel">
  <option value="1">Cat</option>
  <option value="2">Dog</option>
  <option value="3">Fish</option>
</select>

to add a select drop down.

Then we write

document.getElementById("sel").value = "1";

to select the drop down with getElementById.

And we set its value property to the string of the value attribute of the option element we want to select to select it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *