Last updated: Mar 5, 2024
Reading timeยท3 min
To get the value and text of a select element on change:
change
event listener to the select element.value
property to get the value of the element, e.g.
select.value
.text
property to get the text of the element, e.g.
select.options[select.selectedIndex].text
.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <select id="select" style="font-size: 3rem"> <option value="">--Choose an option--</option> <option value="horse">Horse ๐ด</option> <option value="wolf">Wolf ๐บ</option> <option value="fox">Fox ๐ฆ</option> </select> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const select = document.getElementById('select'); select.addEventListener('change', function handleChange(event) { console.log(event.target.value); // ๐๏ธ get selected VALUE // ๐๏ธ get selected VALUE even outside event handler console.log(select.options[select.selectedIndex].value); // ๐๏ธ get selected TEXT in or outside event handler console.log(select.options[select.selectedIndex].text); });
We added a change
event listener to the
select
element.
handleChange
function gets invoked.We used the
target property
on the event
object. The target
property is a reference to the object
(element) on which the event was dispatched.
In the example, the event.target
property points to the select
element,
because that is the element on which the event was dispatched.
select
elementThe value
property allows us to read or set the value of the select
element.
const select = document.getElementById('select'); // โ Read value console.log(select.value); // ๐๏ธ "" // โ Set value select.value = 'fox'; console.log(select.value); // ๐๏ธ "fox"
option
elements.The options
property on the select element returns an array-like object that
contains all of the options of the select element.
const select = document.getElementById('select'); console.log(select.options); // ๐๏ธ [option, option, option, option] select.addEventListener('change', function handleChange(event) { console.log(select.options); // ๐๏ธ [option, option, option, option] });
option
elementWe can use the selectedIndex
property to get the index of the currently
selected option
.
const select = document.getElementById('select'); console.log(select.selectedIndex); select.addEventListener('change', function handleChange(event) { console.log(select.selectedIndex); });
Initially it is set to 0
, but if you console.log
the selectedIndex
in the
handleChange
function and change the selected element, you will see the index
change.
The selectedIndex
property can be used to get the index of the currently
selected option
element. The index can be used to get the element's value
and text
.
const select = document.getElementById('select'); select.addEventListener('change', function handleChange(event) { // ๐๏ธ get selected VALUE even outside event handler console.log(select.options[select.selectedIndex].value); // ๐๏ธ get selected TEXT in or outside an event handler console.log(select.options[select.selectedIndex].text); });
This approach can be used both inside and outside of an event handler function.
I've also written an article on how to set the value of a select element.
You can learn more about the related topics by checking out the following tutorials: