Reputation: 98786
How do you get the currently selected <option>
of a <select>
element via JavaScript?
Upvotes: 79
Views: 139460
Reputation: 329
Or even shorter:
document.getElementById( "your-select-id" ).value
Upvotes: 1
Reputation: 149
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
Upvotes: 1
Reputation: 10801
Using the selectedOptions
property:
var yourSelect = document.getElementById("your-select-id");
alert(yourSelect.selectedOptions[0].value);
It works in all browsers except Internet Explorer.
Upvotes: 8
Reputation: 25675
This will do it for you:
var yourSelect = document.getElementById( "your-select-id" );
alert( yourSelect.options[ yourSelect.selectedIndex ].value )
Upvotes: 129
Reputation: 526493
The .selectedIndex
of the select
object has an index; you can use that to index into the .options
array.
Upvotes: 28