HTML Javascript question
to get the selected value of a input-select I can use
<select id="gender" name="gender" class="style12"> <option selected="selected">ALL</option> <option>Male Only</option> <option>Female Only</option> </select> document.getElementById('gender').value
is there any easy way for me to get the selected label (i.e. ALL / Male Only / Female Only) with javascript?
Thanks a lot for reading.
To get the value of a select or dropdown in HTML using pure JavaScript, first we get the select tag, in this case by id, and then we get the selected value through the selectedIndex property. The value "en" will be printed on the console (Ctrl + Shift + J to open the console).
The id attribute is needed to associate the drop-down list with a label. The <option> tags inside the <select> element define the available options in the drop-down list.
var el = document.getElementById('gender'); var text = el.options[el.selectedIndex].innerHTML;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With