17

How to determine what is selected in the drop down? In Javascript.

Kara
  • 5,996
  • 16
  • 49
  • 56
anonymous
  • 171
  • 1
  • 1
  • 3
  • 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 Answers5

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
  • 25,896
  • 16
  • 64
  • 96
  • 1
    I'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
  • 2
    It works on all browsers that I know of, including IE6. (just tested) – casablanca Oct 27 '10 at 02:13
  • 1
    It 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
  • 68,094
  • 7
  • 131
  • 148
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
  • 16,639
  • 2
  • 38
  • 74
0

Like this:

$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];
Pablo Santa Cruz
  • 170,119
  • 31
  • 233
  • 283