2450

How can I get the selected text (not the selected value) from a drop-down list in jQuery?

kewlashu
  • 999
  • 9
  • 18
haddar
  • 24,707
  • 3
  • 19
  • 12

35 Answers35

3951
$("#yourdropdownid option:selected").text();
rahul
  • 179,909
  • 49
  • 229
  • 260
  • 217
    I think this should be `$("#yourdropdownid").children("option").filter(":selected").text()` since is() returns a boolean of whether the object matches the selector or not. – MHollis May 18 '12 at 14:04
  • 45
    I second the comment about is() returning a boolen; alternatively, use the following small alteration: $('#yourdropdownid').children("option:selected").text(); – scubbo Jun 12 '12 at 14:56
  • 102
    `$('select').children(':selected')` is the fastest way: http://jsperf.com/get-selected-option-text – Simon Apr 24 '13 at 13:51
  • And if have scape caracteres like `\n \t` you can add `.trim()` getting like this: `$("#yourdropdownid option:selected").text().trim();` – Eduardo Leffa Abrantes Apr 22 '22 at 20:39
278

Try this:

$("#myselect :selected").text();

For an ASP.NET dropdown you can use the following selector:

$("[id*='MyDropDownId'] :selected")
kgiannakakis
  • 100,996
  • 27
  • 157
  • 193
229

The answers posted here, for example,

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

didn't work for me, but this did:

$('#yourdropdownid').find('option:selected').text();

It is possibly an older version of jQuery.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
JYX
  • 2,553
  • 2
  • 16
  • 14
113

If you already have the dropdownlist available in a variable, this is what works for me:

$("option:selected", myVar).text()

The other answers on this question helped me, but ultimately the jQuery forum thread $(this + "option:selected").attr("rel") option selected is not working in IE helped the most.

Update: fixed the above link

Nouman Bhatti
  • 1,239
  • 3
  • 29
  • 49
Kirk Liemohn
  • 7,433
  • 8
  • 44
  • 52
75

This works for me

$("#dropdownid").change(function() {
    alert($(this).find("option:selected").text());
});

If the element created dynamically

$(document).on("change", "#dropdownid", function() {
    alert($(this).find("option:selected").text());
});
Prabhagaran
  • 3,370
  • 1
  • 18
  • 18
68
$("option:selected", $("#TipoRecorde")).text()
Muhammad Usman
  • 12,212
  • 6
  • 35
  • 58
Rafael
  • 681
  • 5
  • 2
55

This works for me:

$('#yourdropdownid').find('option:selected').text();

jQuery version: 1.9.1

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Binita Bharati
  • 4,026
  • 1
  • 34
  • 22
54

$("#DropDownID").val() will give the selected index value.

Aziz Shaikh
  • 15,647
  • 11
  • 58
  • 78
Neeraj
  • 685
  • 5
  • 2
  • 40
    Not exactly the answer to the question, but was useful for me. The question wants the selected text. – Peter Nov 28 '11 at 14:50
44

For the text of the selected item, use:

$('select[name="thegivenname"] option:selected').text();

For the value of the selected item, use:

$('select[name="thegivenname"] option:selected').val();
Milap
  • 6,466
  • 8
  • 24
  • 45
Kamrul
  • 6,939
  • 3
  • 28
  • 31
36

Use this

const select = document.getElementById("yourSelectId");

const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;   

Then you get your selected value and text inside selectedValue and selectedText.

Justin Lessard
  • 9,017
  • 4
  • 47
  • 56
Mohammed Shaheen MK
  • 1,109
  • 10
  • 9
35

Various ways

1. $("#myselect option:selected").text();

2. $("#myselect :selected").text();

3. $("#myselect").children(":selected").text();

4. $("#myselect").find(":selected").text();
MaxEcho
  • 13,989
  • 6
  • 76
  • 86
32
$("#dropdownID").change(function(){
  alert($('option:selected', $(this)).text());
});
124
  • 2,659
  • 24
  • 37
30
var someName = "Test";

$("#<%= ddltest.ClientID %>").each(function () {
    $('option', this).each(function () {
        if ($(this).text().toLowerCase() == someName) {
            $(this).attr('selected', 'selected')
        };
    });
});

That will help you to get right direction. Above code is fully tested if you need further help let me know.

Vivek
  • 10,682
  • 14
  • 46
  • 65
Zarni
  • 317
  • 3
  • 2
20

For those who are using SharePoint lists and don't want to use the long generated id, this will work:

var e = $('select[title="IntenalFieldName"] option:selected').text();
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
FAA
  • 499
  • 1
  • 7
  • 15
17
 $("#selectID option:selected").text();

Instead of #selectID you can use any jQuery selector, like .selectClass using class.

As mentioned in the documentation here.

The :selected selector works for <option> elements. It does not work for checkboxes or radio inputs; use :checked for them.

.text() As per the documentation here.

Get the combined text contents of each element in the set of matched elements, including their descendants.

So you can take text from any HTML element using the .text() method.

Refer the documentation for a deeper explanation.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Nikhil Agrawal
  • 25,020
  • 19
  • 86
  • 120
16
$("select[id=yourDropdownid] option:selected").text()

This works fine

Thangamani Palanisamy
  • 4,962
  • 3
  • 31
  • 39
13
$('#id').find('option:selected').text();
Nikul
  • 1,015
  • 1
  • 12
  • 31
13

For getting selected value use

$('#dropDownId').val();

and for getting selected item text use this line:

$("#dropDownId option:selected").text();
Mojtaba
  • 494
  • 9
  • 18
13

This code worked for me.

$("#yourdropdownid").children("option").filter(":selected").text();
Naveenbos
  • 2,486
  • 3
  • 32
  • 60
  • Works too! And if have scape caracteres like `\n \t` you can add `.trim()` getting like this: `$("#yourdropdownid").children("option").filter(":selected").text().trim();` – Eduardo Leffa Abrantes Apr 22 '22 at 20:39
12

Select Text and selected value on dropdown/select change event in jQuery

$("#yourdropdownid").change(function() {
    console.log($("option:selected", this).text()); //text
    console.log($(this).val()); //value
})
Milap
  • 6,466
  • 8
  • 24
  • 45
Sandeep Shekhawat
  • 647
  • 1
  • 9
  • 19
12

Try:

$var = jQuery("#dropdownid option:selected").val();
   alert ($var);

Or to get the text of the option, use text():

$var = jQuery("#dropdownid option:selected").text();
   alert ($var);

More Info:

Vishal Thakur
  • 1,336
  • 15
  • 24
11
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
Kalaivani M
  • 1,200
  • 14
  • 28
11

Simply try the following code.

var text= $('#yourslectbox').find(":selected").text();

it returns the text of the selected option.

Muddasir23
  • 553
  • 6
  • 15
9

Use:

('#yourdropdownid').find(':selected').text();
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
kishore
  • 1,580
  • 6
  • 23
  • 32
8

the following worked for me:

$.trim($('#dropdownId option:selected').html())
Mohammad Dayyan
  • 20,033
  • 39
  • 154
  • 219
7

In sibling case

<a class="uibutton confirm addClient" href="javascript:void(0);">ADD Client</a>
<input type="text" placeholder="Enter client name" style="margin: 5px;float: right" class="clientsearch large" />
<select class="mychzn-select clientList">
  <option value="">Select Client name....</option>
  <option value="1">abc</option>
</select>


 /*jQuery*/
 $(this).siblings('select').children(':selected').text()
Milap
  • 6,466
  • 8
  • 24
  • 45
vineet
  • 12,735
  • 10
  • 52
  • 73
7

This work for me:

$("#city :selected").text();

I'm using jQuery 1.10.2

6

$(function () {
  alert('.val() = ' + $('#selectnumber').val() + '  AND  html() = ' + $('#selectnumber option:selected').html() + '  AND .text() = ' + $('#selectnumber option:selected').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>

  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        <select id="selectnumber">
          <option value="1">one</option>
          <option value="2">two</option>
          <option value="3">three</option>
          <option value="4">four</option>
        </select>

      </div>
    </form>
  </body>
</html>

Click to see OutPut Screen

Satinder singh
  • 9,714
  • 16
  • 57
  • 99
Bhanu Pratap
  • 1,453
  • 14
  • 15
6
$("#dropdown").find(":selected").text();


$("#dropdown :selected").text();

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

$("#dropdown").children(":selected").text();
shyamm
  • 456
  • 7
  • 15
4

If you want the result as a list, then use:

x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})
max
  • 8,927
  • 15
  • 81
  • 134
3

Try

dropdown.selectedOptions[0].text

function read() {
  console.log( dropdown.selectedOptions[0].text );
}
<select id="dropdown">
  <option value="1">First</option>
  <option value="2">Second</option>
</select>
<button onclick="read()">read</button>
Kamil Kiełczewski
  • 71,169
  • 26
  • 324
  • 295
2

For multi-selects:

$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
Curtis Yallop
  • 5,990
  • 3
  • 42
  • 29
2
$("#dropdownid option:selected").text();

if you use asp.net and write

<Asp:dropdownlist id="ddl" runat="Server" />

then you should use

$('#<%=ddl.Clientid%> option:selected').text();
Manish Singh
  • 884
  • 1
  • 12
  • 26
0

Just add the below line

$(this).prop('selected', true);

replaced .att to .prop it worked for all browsers.

Nirav Vasoya
  • 356
  • 3
  • 18
-1
$("#dropdownid").change(function(el) {
    console.log(el.value);
});

Or you can use this

$("#dropdownid").change(function() {
    console.log($(this).val());
});
Dhiaa Shalabi
  • 1,040
  • 8
  • 19