Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  19] [ 3]  / answers: 1 / hits: 30371  / 12 Years ago, tue, january 15, 2013, 12:00:00

I have the following Element:



<select id=color name=colorId class=btn secondary>
<option value=347366 selected=selected>Purple</option>
<option value=56634>White</option>
</select>


And I want to find which option is selected:



The following give me only the default:



document.querySelector('#color option[selected=selected]')


(I know how to do it with JQuery but, I can't use jQuery or any other similar library)


More From » html

 Answers
18

In plain javascript:



var select = document.getElementById('color');
var currentOpt = select.options[select.selectedIndex];


JsBin example: http://jsbin.com/ogunet/1/edit (open your js console)


[#80871] Sunday, January 13, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarissakourtneyb

Total Points: 710
Total Questions: 89
Total Answers: 125

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;