Last updated: Mar 5, 2024
Reading timeยท3 min
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.
<!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>
And here is the related JavaScript code.
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'; });
We used the value
property to set the value of a
select
element.
option
element be an empty string.To remove the selection, set the value
property of the select element to an
empty string.
const select = document.getElementById('fruit-select'); // โ set select value select.value = 'apple'; console.log(select.value); // ๐๏ธ "apple" // โ unset select value select.value = '';
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
.
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']
select
elementThe value of a select
element can be changed in the same way it is set, just
update the value
property.
const select = document.getElementById('fruit-select'); select.value = 'apple'; console.log(select.value); // ๐๏ธ "apple" select.value = 'banana'; console.log(select.value); // ๐๏ธ "banana"
option
elements, the value of the select
element gets reset.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.
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"
It also helps the readers of your code know what the alternative values of the
select
element are.
You can learn more about the related topics by checking out the following tutorials: