To change the selected option in an HTML <select> element we have to change the value property on the <select> element or the selected property on the <option> element we want to get selected.

There are different ways we can do it and choosing one or another will depend on the information we have available to us. It can be the text that gets displayed on the option element, its index, its id etc.

Let's see 6 different ways of changing the selected option using JavaScript:

1. Changing selected option by value

In order to change the selected option by the value attribute, all we have to do is change the value property of the <select> element.

The select box will then update itself to reflect the state of this property. This is the easiest and most straightforward way of updating a select box state.

Let's say that we want to select the option containing the attribute value="steve".

<select id="mySelect">
<option value="ann">Ann Frank</option>
<option value="paul" selected>Paul Allen</option>
<option value="steve">Steve Jobs</option>
</select>

All we have to do is set the value of the select box to that same value:

const changeSelected = (e) => {
const $select = document.querySelector('#mySelect');
$select.value = 'steve'
};

See the Pen on CodePen.

2. Changing selected option by option text

We might also want to change the value of a select box by the text of the <option> element that gets displayed to visitors.

Doing this is probably the most complicated way, but still, something relatively simple to do. We have to iterate over the options and compare their text with the text of the element we want to select.

Once we find the element we want to update, we'll be updating its selected property and setting it to true. This way, the select box will also update itself to reflect this change.

Note: this only works for select box elements that only admit a single value. If you are using a select box admiting multiple selected elements this will just add another option too the multi-select instead of changing the active one for another.

const changeSelected = (e) => {
const text = 'Steve Jobs';
const $select = document.querySelector('#mySelect');
const $options = Array.from($select.options);
const optionToSelect = $options.find(item => item.text ===text);
optionToSelect.selected = true;
};

See the Pen on CodePen.

There's another way of doing this and that will solve the problem with multi-select inputs. Basically, once we find the element that has the text we are looking for, instead of just changing its selected property, we'll be getting it's value attribute and using it to set it as the only value for the whole select box.

const changeSelected = (e) => {
const text = 'Steve Jobs';
const $select = document.querySelector('#mySelect');
const $options = Array.from($select.options);
const optionToSelect = $options.find(item => item.text ===text);

// Here's the trick:
$select.value = optionToSelect.value;
};

See the Pen on CodePen.

3. Changing selected option by option id, class, or attribute.

This is quite simple too. This solution will also work for multi-select inputs by assigning a single value to it:

const changeSelected = (e) => {
const $select = document.querySelector('#mySelect');
const $option = $select.querySelector('#myId');
$select.value = $option.value;
};

See the Pen on CodePen.

And of course, it can be done in the same way if we have a class attribute instead of an id or any other attribute like data-selected="true". Just change the query to get the option element:

// Using id
const $option = $select.querySelector('#myId');

// Using classname
const $option = $select.querySelector('#mySelect .myId');

// Using data-attribute
const $option = $select.querySelector('#mySelect [data-selected="myElement"]');

4. Changing selected option by index

If all we have is the index of the option element, we can set the selected attribute by retrieving the select box options. Then we only have to select the one we need from the array by its index.

Notice that the index is 0 based. So, the first <option> element will have index 0, the next 1, and so forth.

Knowing this, if we want to select the 3rd element, we'll be using index 2 in our code to select it:

// Selecting the 3rd option (Demo 2)
document.querySelector('#mySelect').querySelector('option')[2].selected = 'selected'

Let's see it working on a click event:

See the Pen on CodePen.

And rewritten to work for multi-select inputs:

// Selecting the 3rd option (Demo 2)
const $select = document.querySelector('#mySelect');
$select.value = $select.querySelector('option')[2].value;

Conclusion

Remember that, no matter what way you choose to use to replace and change the selected element on your <select> elements, you'd be better of by updating the value property if you want a single element to be selected. Otherwise, when having select boxes admitting multiple elements you won't replace the active element but just add another one.

If you are learning JavaScript and this seems a bit difficult for you, there's nothing to worry about! Eventually, you'll get there. See how long does it take to learn JavaScript and get motivated!