Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected Item from the list with JS [duplicate]

Tags:

javascript

I have an html list with some items loaded. I am able to get the select list object using the following code:

var list = document.getElementById('ddlReason');

but I need help with figuring out how to detect which value has been chosen from the list.

like image 209
Alex Avatar asked Feb 19 '10 14:02

Alex


1 Answers

// Gets your select
var list = document.getElementById('ddlReason');

// Get the index of selected item, first item 0, second item 1 etc ...
var INDEX = list.selectedIndex;

// Viola you're done
alert(list[INDEX].value);

Edit (forgot .value).

You can also make that a bit more concise, but I wanted to make it readable so you could see what was going on. Shorter version:

var list = document.getElementById('ddlReason');
alert(list[list.selectedIndex].value);
like image 88
Erik Avatar answered Oct 15 '22 12:10

Erik