How to determine what is selected in the drop down? In Javascript.
Asked
Active
Viewed 1e+01k times
17
-
possible duplicate of [how to get selected value of dropdownlist using javascript?](http://stackoverflow.com/questions/1085801/how-to-get-selected-value-of-dropdownlist-using-javascript) – Crescent Fresh Oct 27 '10 at 02:07
-
It's usually quite helpful if you include some code in your questions – kurdtpage Jan 09 '17 at 00:13
5 Answers
35
If your dropdown is something like this:
<select id="thedropdown">
<option value="1">one</option>
<option value="2">two</option>
</select>
Then you would use something like:
var a = document.getElementById("thedropdown");
alert(a.options[a.selectedIndex].value);
But a library like jQuery simplifies things:
alert($('#thedropdown').val());

cambraca
- 27,014
- 16
- 68
- 99
-
1I'm digging deep in my memory here, but I think `a.value` didn't work in some browsers (probably IE 6, haha). Anyway, using a library is best. – cambraca Oct 27 '10 at 01:43
-
2It works on all browsers that I know of, including IE6. (just tested) – casablanca Oct 27 '10 at 02:13
-
1It happened in very old browsers like Netscape Navigator 4. [Look](http://bytes.com/topic/javascript/answers/90872-how-get-selected-value-select-using-dom) – cambraca Oct 27 '10 at 02:33
7
Use the value
property of the <select>
element. For example:
var value = document.getElementById('your_select_id').value;
alert(value);

casablanca
- 69,683
- 7
- 133
- 150
6
<select onchange = "selectChanged(this.value)">
<item value = "1">one</item>
<item value = "2">two</item>
</select>
and then the javascript...
function selectChanged(newvalue) {
alert("you chose: " + newvalue);
}

Thomas F.
- 134
- 3
1
var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;

Soufiane Hassou
- 17,257
- 2
- 39
- 75
0
Like this:
$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];

Pablo Santa Cruz
- 176,835
- 32
- 241
- 292