How do you get the currently selected <option>
of a <select>
element via JavaScript?
Asked
Active
Viewed 1.3e+01k times
73

ROMANIA_engineer
- 54,432
- 29
- 203
- 199

Paul D. Waite
- 96,640
- 56
- 199
- 270
-
1Possible duplicate: [How to get the selected value of dropdownlist using JavaScript?](http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-of-dropdownlist-using-javascript) – Anderson Green May 26 '13 at 07:33
-
2@AndersonGreen: that isn’t *quite* the same question. It’s actually asking how to get the text of the selection ` – Paul D. Waite May 26 '13 at 08:49
4 Answers
122
This will do it for you:
var yourSelect = document.getElementById( "your-select-id" );
alert( yourSelect.options[ yourSelect.selectedIndex ].value )

Pat
- 25,237
- 6
- 71
- 68
-
15if you use it to access the value of the option, you may as well just use `yourSelect.value`. Very old, archaic browsers might not support it, but IE6 and every modern browser does. – Andy E Jul 21 '10 at 16:52
-
@Andy E: ooh, there’s an idea — that is indeed what I’m doing, and your code is a bit easier to read. – Paul D. Waite Jul 21 '10 at 17:02
-
@Andy E: ah, one caveat about that method — it doesn’t return the text of the ` – Paul D. Waite Jul 29 '10 at 16:24
26
The .selectedIndex
of the select
object has an index; you can use that to index into the .options
array.
5
Using the selectedOptions
property:
var yourSelect = document.getElementById("your-select-id");
alert(yourSelect.selectedOptions[0].value);
It works in all browsers except Internet Explorer.

Finesse
- 9,793
- 7
- 62
- 92
-
3“It works in all browsers except Internet Explorer.” As soon as everyone stops using Internet Explorer, we’re golden! [Possible polyfill here](https://gist.github.com/brettz9/4212217). – Paul D. Waite Jan 31 '18 at 08:42
1
if you don't have multiple selection You can do it like.
var yourSelect = document.getElementById( "your-select-id" );
alert(yourSelect.selectedOptions[0].value)
Will do the same thing like the choosen answer. Since .selectedOptions
gives you a list of selected options elements

jasjastone
- 11
- 3
-
1Sure, although it's already mentioned in [this answer](https://stackoverflow.com/a/48534292/20578) – Paul D. Waite Apr 03 '23 at 19:05