0

This gets the value of whatever is selected in my dropdown menu.

document.getElementById('tester').value

I cannot however find out what property to go after for the text that's currently displayed by the drop down menu. I tried "text" then looked at W3Schools but that didn't have the answer, does anybody here know?

For those not sure, here's the HTML for a drop down box.

<select name="tester" id="tester">
    <option value="1">A skill</option>
    <option value="2">Another skill</option>
    <option value="3">Yet another skill</option>
</select>

Thanks

Epik
  • 3,327
  • 4
  • 17
  • 23
  • for jQuery checkout [http://stackoverflow.com/questions/1643227/jquery-get-selected-text-from-dropdownlist](http://stackoverflow.com/questions/1643227/jquery-get-selected-text-from-dropdownlist). For pure JavaScript check out http://stackoverflow.com/questions/5913/getting-the-text-from-a-drop-down-box. – cbayram Nov 05 '12 at 23:31

3 Answers3

3

Try

var tester = document.getElementById('tester');
var text = tester.options[​​​tester.selectedIndex].innerHTML​;

DEMO

Musa
  • 96,336
  • 17
  • 118
  • 137
  • Thank you that is what i was looking for awesome nice work and very fast return! – Epik Nov 05 '12 at 23:39
  • An `option` DOM object does have a `text` property, so `.text` should work here too: `var text = tester.options[​​​tester.selectedIndex].text;` – Kevin Boucher Nov 05 '12 at 23:41
1

Text looks good to me - jsFiddle

$('#tester').on('change', function(){
    console.log($('option:selected', this).text()); 
});​
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
1

I think you want something like this:

var d = document.getElementById('tester');
var selected_text = d.options[d.selectedIndex].text;

(this is untested, but your answer should be similar to this)

DorkRawk
  • 732
  • 2
  • 6
  • 21