Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected label from a html <select>?

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.

like image 512
Unreality Avatar asked May 24 '09 13:05

Unreality


People also ask

How can we get the selected option value of select tag in HTML?

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).

How do I select a label in HTML?

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.


1 Answers

var el = document.getElementById('gender'); var text = el.options[el.selectedIndex].innerHTML; 
like image 85
Gideon Avatar answered Sep 23 '22 23:09

Gideon