Skip to content Skip to sidebar Skip to footer

Get Options Value Using Tag Name In JavaScript

I have this HTML I want to

Solution 1:

document.getElementsByTagName() returns an HTMLCollection(which is a kind of an array), so it does have the options properties. You can access the items in the list using indexing.

var parent = document.getElementsByTagName('select')[0];
var tempUnit = parent.options[parent.selectedIndex].value;

document.querySelector('#result').innerHTML = tempUnit
<select id="tempSelect" >
    <option value="F">Fahrenheit</option>
    <option value="C" selected>Celsius</option>
</select>
<div id="result"></div>

Post a Comment for "Get Options Value Using Tag Name In JavaScript"