anonymous
anonymous

Reputation: 171

Get drop down value

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

Upvotes: 17

Views: 104039

Answers (5)

Thomas F.
Thomas F.

Reputation: 134

<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);
}

Upvotes: 6

casablanca
casablanca

Reputation: 70721

Use the value property of the <select> element. For example:

var value = document.getElementById('your_select_id').value;
alert(value);

Upvotes: 7

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

Like this:

$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];

Upvotes: 0

cambraca
cambraca

Reputation: 27837

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());

Upvotes: 35

Related Questions