Set the Value of a Select Element using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
3 min

banner

# Set the Value of a Select Element using JavaScript

Use the value property to set the value of a select element, e.g. select.value = 'new value'.

The value property can be used to set or update the value of a select element. To remove the selection, set the value to an empty string.

Here is the HTML for the examples.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body style="margin-top: 60px; margin-left: 100px"> <select name="fruits" id="fruit-select"> <option value="">--Choose an option--</option> <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="kiwi">Kiwi</option> </select> <button style="margin-top: 10px" id="btn">Click</button> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

And here is the related JavaScript code.

index.js
const select = document.getElementById('fruit-select'); // โœ… set select value select.value = 'apple'; console.log(select.value); // ๐Ÿ‘‰๏ธ "apple" // โœ… unset select value // select.value = ''; // -------------------------------- const allOptions = Array.from(select.options).map( option => option.value, ); console.log(allOptions); // ๐Ÿ‘‰๏ธ ['', 'apple', 'banana', 'kiwi'] // -------------------------------- // โœ… get select value on change select.addEventListener('change', function handleChange(event) { console.log(event.target.value); // ๐Ÿ‘‰๏ธ get selected VALUE }); // -------------------------------- // โœ… Set select element value on button click const btn = document.getElementById('btn'); btn.addEventListener('click', function handleClick() { select.value = 'banana'; });

set select element value

The code for this article is available on GitHub

We used the value property to set the value of a select element.

A convention for when you don't have a default value is to have the value of the first option element be an empty string.

# Removing the selection

To remove the selection, set the value property of the select element to an empty string.

index.js
const select = document.getElementById('fruit-select'); // โœ… set select value select.value = 'apple'; console.log(select.value); // ๐Ÿ‘‰๏ธ "apple" // โœ… unset select value select.value = '';

# Getting an array of the values of all option elements

If you need an array of the values of all the option elements, use the map() method to iterate over the elements and return the value of each option.

index.js
const select = document.getElementById('fruit-select'); // โœ… set select value select.value = 'apple'; console.log(select.value); // ๐Ÿ‘‰๏ธ "apple" const allOptions = Array.from(select.options).map(option => option.value); console.log(allOptions); // ๐Ÿ‘‰๏ธ ['', 'apple', 'banana', 'kiwi']
The code for this article is available on GitHub

# Changing the value of a select element

The value of a select element can be changed in the same way it is set, just update the value property.

index.js
const select = document.getElementById('fruit-select'); select.value = 'apple'; console.log(select.value); // ๐Ÿ‘‰๏ธ "apple" select.value = 'banana'; console.log(select.value); // ๐Ÿ‘‰๏ธ "banana"
The code for this article is available on GitHub
If you set a select element's value to a value that is not present among theoption elements, the value of the select element gets reset.
index.js
const select = document.getElementById('fruit-select'); select.value = 'does-not-exist'; console.log(select.value); // ๐Ÿ‘‰๏ธ ""

You can create an object that stores the values of the option elements to avoid misspelling values.

index.js
const select = document.getElementById('fruit-select'); const values = { apple: 'apple', banana: 'banana', kiwi: 'kiwi', }; select.value = values.apple; console.log(select.value); // ๐Ÿ‘‰๏ธ "apple" select.value = values.banana; console.log(select.value); // ๐Ÿ‘‰๏ธ "banana"
The code for this article is available on GitHub
This is a much better solution than hard-coding strings all over the place because it leverages your IDE's autocompletion.

It also helps the readers of your code know what the alternative values of the select element are.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev