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.

0

4 Answers 4

3
document.getElementById('short_code').value
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

0
document.getElementById('short_code').options[document.getElementById('short_code').selectedIndex].text

Comments

0

Try this:

var el = document.getElementById("short_code");
var code = el.options[el.selectedIndex].value;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.