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
  • 960
  • 6
  • 16
  • 33

4 Answers4

3
document.getElementById('short_code').value
Mihai Matei
  • 24,166
  • 5
  • 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
  • 5,036
  • 2
  • 18
  • 29
0
document.getElementById('short_code').options[document.getElementById('short_code').selectedIndex].text
Codesen
  • 7,724
  • 5
  • 29
  • 31
0

Try this:

var el = document.getElementById("short_code");
var code = el.options[el.selectedIndex].value;
Barry Kaye
  • 7,682
  • 6
  • 42
  • 64