19

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.

Community
  • 1
  • 1
guitarlass
  • 1,587
  • 7
  • 21
  • 45

4 Answers4

34

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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
15
$("#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();
Vikrant
  • 4,920
  • 17
  • 48
  • 72
neohope
  • 1,822
  • 15
  • 29
2

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);
}
Dhrumil Bhankhar
  • 1,301
  • 1
  • 14
  • 15
1

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

Neysor
  • 3,893
  • 11
  • 34
  • 66
  • 2
    CSS3? Where does that come into it? – Rory McCrossan May 02 '12 at 10:07
  • Indeed, `:selected` is not CSS3, it's one of the jQuery selector enhancments. – T.J. Crowder May 02 '12 at 10:12
  • 1
    _"Because `:selected` is a jQuery extension and not part of the CSS specification, queries using `:selected` cannot ... ..."_ [source](http://api.jquery.com/selected-selector/) – gdoron May 02 '12 at 10:14
  • oh thanks for this correction! i thought :selected and :checked are css3 pseudo classes. And i know that jquery is just [borrowing](http://api.jquery.com/category/selectors/) css selectors :) – Neysor May 02 '12 at 10:15