Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  17] [ 5]  / answers: 1 / hits: 87094  / 14 Years ago, mon, february 21, 2011, 12:00:00

I have something like this:



select = document.getElementById(select);
select.onchange = function(){
alert(this.value); //returns the selected value
alert(this.innerHTML); //returns the entire select with all the options
alert(this.selected.innerHTML); //this is what I want, but doesn't work, of course
};


How can I get the innerHTML of the selected option in pure js? (no frameworks).


More From » dom

 Answers
20

Try:



alert(this.options[this.selectedIndex].text);


Demo:





<select onchange=alert(this.options[this.selectedIndex].text)>
<option>foo
<option>bar
<option>foobar
</select>




[#93644] Saturday, February 19, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
;