-3

Possible Duplicate:
How to get the selected value of dropdownlist using JavaScript?
How to get the value of a selected text in javascript

<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 need to do this:

if(document.getElementById("short_code").options.item(document.getElementById("short_code").selectedIndex).text)== "First")

//get the value of the option Fist , how to get the value?
Community
  • 1
  • 1
alkhader
  • 960
  • 6
  • 16
  • 33
  • 5
    You [just asked about this](http://stackoverflow.com/questions/11828125/how-to-get-the-value-of-a-selected-text-in-javascript). If the answers don't work, you need to clarify your question instead of reposting. –  Aug 06 '12 at 12:45

2 Answers2

12
var elem = document.getElementById("short_code"),
    selectedNode = elem.options[elem.selectedIndex];

if ( selectedNode.value === "First" ) { //...
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
2

How about this:

var select = document.getElementById('short_code');
var options = select.options;
var selected = select.options[select.selectedIndex];
//which means that:
console.log(selected.value || selected.getAttribute('value'));//should log "12"
console.log(selected.innerHTML);//should log(First);

Loop through all the options (either creating a reference variable, like I did with options, or plain for (var i=0;i<select.options.length;i++)) and you can get all info you need

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149