Paul D. Waite
Paul D. Waite

Reputation: 98786

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

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

Upvotes: 79

Views: 139460

Answers (5)

Don Cesar D&#39;Bazar
Don Cesar D&#39;Bazar

Reputation: 329

Or even shorter:

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

Upvotes: 1

jasjastone
jasjastone

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

Finesse
Finesse

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

Pat
Pat

Reputation: 25675

This will do it for you:

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

Upvotes: 129

Amber
Amber

Reputation: 526493

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

Upvotes: 28

Related Questions