Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text of the selected value of a dropdown list? [duplicate]

Tags:

jquery

select

Possible Duplicate:
jQuery get specific option tag text
How to get the text of the selected option of a select using jquery?

I have a dropdown list and I want to know the text of the selected item. For example:

<select>
    <option value="1">Volvo</option>
    <option value="2">Saab</option>
    <option value="3">Mercedes</option>
</select>

If I know the selected value, how can I get it's text value? For instance, if the value is 1 how can I get Volvo?

Help much appreciated.

like image 505
guitarlass Avatar asked May 02 '12 10:05

guitarlass


People also ask

What is the easiest way to copy the contents of a drop down list from a website?

For Chrome:Right Click on HTML Dropdownlist, Select Inspect Element and In Developer Tools, you will see html source is selected. Right click and click Copy as HTML option.


4 Answers

You can use option:selected to get the chosen option of the select element, then the text() method:

$("select option:selected").text();

Here's an example:

console.log($("select option:selected").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select>
    <option value="1">Volvo</option>
    <option value="2" selected="selected">Saab</option>
    <option value="3">Mercedes</option>
</select>
like image 72
Rory McCrossan Avatar answered Oct 10 '22 18:10

Rory McCrossan


$("#select_id").find("option:selected").text();

It is helpful if your control is on Server side. In .NET it looks like:

$('#<%= dropdownID.ClientID %>').find("option:selected").text();
like image 45
neohope Avatar answered Oct 10 '22 19:10

neohope


Hi if you are having dropdownlist like this

<select id="testID">
<option value="1">Value1</option>
<option value="2">Value2</option>
<option value="3">Value3</option>
<option value="4">Value4</option>
<option value="5">Value5</option>
<option value="6">Value6</option>
</select>
<input type="button" value="Get dropdown selected Value" onclick="getHTML();">

after giving id to dropdownlist you just need to add jquery code like this

function getHTML()
{
      var display=$('#testID option:selected').html();
      alert(display);
}
like image 28
Dhrumil Bhankhar Avatar answered Oct 10 '22 19:10

Dhrumil Bhankhar


The easiest way is through css3 $("select option:selected") and then use the .text() or .html() function. depending on what you want to have.

like image 1
Neysor Avatar answered Oct 10 '22 19:10

Neysor