How to determine what is selected in the drop down? In Javascript.
-
possible duplicate of how to get selected value of dropdownlist using javascript?Crescent Fresh– Crescent Fresh2010年10月27日 02:07:21 +00:00Commented Oct 27, 2010 at 2:07
-
It's usually quite helpful if you include some code in your questionskurdtpage– kurdtpage2017年01月09日 00:13:53 +00:00Commented Jan 9, 2017 at 0:13
5 Answers 5
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
cambraca
28k17 gold badges71 silver badges102 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
cambraca
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.casablanca
It works on all browsers that I know of, including IE6. (just tested)
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
casablanca
70.9k8 gold badges139 silver badges155 bronze badges
Comments
<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);
}
Comments
var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;
answered Oct 27, 2010 at 1:24
Soufiane Hassou
17.8k2 gold badges42 silver badges76 bronze badges
Comments
Like this:
$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];
answered Oct 27, 2010 at 1:24
Pablo Santa Cruz
182k33 gold badges250 silver badges300 bronze badges
Comments
default