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)
Grab the
<select>
DOM element usinggetElementById()
and take its parameterselectedIndex
:You can do this with
querySelectorAll
notquerySelector
or
This will return selected option value and text both.. Hope this will work for u ..
Cheers
Use the :checked selector. This applies to checkbox, radio, and select
for the node
for the value
In plain javascript:
JsBin example: http://jsbin.com/ogunet/1/edit (open your js console)