Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the text from a drop-down box

This gets the value of whatever is selected in my dropdown menu.

document.getElementById('newSkill').value 

I cannot however find out what property to go after for the text that's currently displayed by the drop down menu. I tried "text" then looked at W3Schools but that didn't have the answer, does anybody here know?

For those not sure, here's the HTML for a drop down box.

<select name="newSkill" id="newSkill">     <option value="1">A skill</option>     <option value="2">Another skill</option>     <option value="3">Yet another skill</option> </select> 
like image 758
Teifion Avatar asked Aug 08 '08 13:08

Teifion


2 Answers

Based on your example HTML code, here's one way to get the displayed text of the currently selected option:

var skillsSelect = document.getElementById("newSkill"); var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text; 
like image 167
Patrick McElhaney Avatar answered Oct 15 '22 21:10

Patrick McElhaney


Simply You can use jQuery instead of JavaScript

$("#yourdropdownid option:selected").text(); 

Try This.

like image 24
BOBIN JOSEPH Avatar answered Oct 15 '22 21:10

BOBIN JOSEPH