17

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

Kara
6,23616 gold badges54 silver badges58 bronze badges
asked Oct 27, 2010 at 1:19
2

5 Answers 5

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());
answered Oct 27, 2010 at 1:24
Sign up to request clarification or add additional context in comments.

3 Comments

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.
It works on all browsers that I know of, including IE6. (just tested)
It happened in very old browsers like Netscape Navigator 4. Look
7

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

var value = document.getElementById('your_select_id').value;
alert(value);
answered Oct 27, 2010 at 1:26

Comments

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);
}
answered Oct 27, 2010 at 1:41

Comments

1
var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;
answered Oct 27, 2010 at 1:24

Comments

0

Like this:

$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];
answered Oct 27, 2010 at 1:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.