0

Possible Duplicate:
How to get the selected value of dropdownlist using JavaScript?

I have a select:

<select id="short_code">
<option value="12">First</option>
<option value="11">Second</option>
<option value="10">Third</option>
<option value="9">Fourth</option>    
</select>

I want to get the value of the selected text. e.g. if the selected text is First so the I need to get 12.

Community
  • 1
  • 1
alkhader
  • 920
  • 6
  • 15
  • 33

4 Answers4

3
document.getElementById('short_code').value
Mihai Matei
  • 23,635
  • 4
  • 32
  • 50
2

This should do it:

<script type="text/javascript">
    function getSelected(select) {
        alert(select.options[select.selectedIndex].value);
    }
</script>    

<select id="short_code" onchange="getSelected(this)">    
    <option value="12">First</option>
    <option value="11">Second</option>
    <option value="10">Third</option>
    <option value="9">Fourth</option>    
</select>
Chris Clower
  • 4,946
  • 2
  • 16
  • 27
0
document.getElementById('short_code').options[document.getElementById('short_code').selectedIndex].text
Codesen
  • 7,404
  • 5
  • 27
  • 31
0

Try this:

var el = document.getElementById("short_code");
var code = el.options[el.selectedIndex].value;
Barry Kaye
  • 7,442
  • 6
  • 39
  • 62