83

How do you get the currently selected <option> of a <select> element via JavaScript?

2

5 Answers 5

132

This will do it for you:

var yourSelect = document.getElementById( "your-select-id" ); alert( yourSelect.options[ yourSelect.selectedIndex ].value ) 
Sign up to request clarification or add additional context in comments.

3 Comments

if 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: ooh, there’s an idea — that is indeed what I’m doing, and your code is a bit easier to read.
@Andy E: ah, one caveat about that method — it doesn’t return the text of the <option> element in IE, I think you need to have something in the value attribute.
28

The .selectedIndex of the select object has an index; you can use that to index into the .options array.

1 Comment

This is the most elegant and straightforward approach
9

Using the selectedOptions property:

var yourSelect = document.getElementById("your-select-id"); alert(yourSelect.selectedOptions[0].value); 

It works in all browsers except Internet Explorer.

2 Comments

“It works in all browsers except Internet Explorer.” As soon as everyone stops using Internet Explorer, we’re golden! Possible polyfill here.
This only works if the option has the attribute selected (which is normally not the case when you simply select a different option). From MDN: "An option is considered selected if it has an HTMLOptionElement.selected attribute."
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

1 Comment

Sure, although it's already mentioned in this answer
1

Or even shorter:

document.getElementById( "your-select-id" ).value 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.