2387

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

kewlashu
  • 929
  • 9
  • 18
haddar
  • 24,077
  • 3
  • 18
  • 12

34 Answers34

3877
$("#yourdropdownid option:selected").text();
rahul
  • 174,563
  • 47
  • 223
  • 254
  • 212
    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
  • 44
    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
  • 25
    @DT3 tested `is("selected").text()` returns a `TypeError: Object false has no method 'text'` – ianace Oct 19 '12 at 07:20
  • 2
    @DT3, he's not wrong about the speeds. [Here are some quick stats](http://grabilla.com/02c04-34774c6b-bb36-4324-878b-25f4ba6ca94d.html) run on a realistically sized dropdown (8 or so). – Adam Tomat Dec 04 '12 at 15:20
  • 98
    `$('select').children(':selected')` is the fastest way: http://jsperf.com/get-selected-option-text – Simon Apr 24 '13 at 13:51
  • 1
    @Sandeep .val() will return the value of the option element, whereas the OP wants the text contained in the option element: e.g. for the following 'foo' rather than 'bar' is wanted – jinglesthula Mar 18 '14 at 18:04
  • 3
    $("#IdSelect").children("option:selected").text() – JD - DC TECH Nov 18 '14 at 23:19
  • 2
    I'll add this to the mix, especially if you already have a handle to the dropdown element: $select.find("option:selected").text(); – Draghon Jun 17 '16 at 16:10
  • Hi how do you do this inside a Table row/cell, how do you know which of the Dropdwon from the table – Transformer Mar 12 '17 at 07:40
  • Just to add up to the post, is you need it by the field name would be like this: $("[name='input_name'] option:selected").text(); – viniciusalvess Jul 02 '17 at 15:44
  • @MHollis your way is good but too long. this method is useful when we have some dynamic elements on document. – Hemant Sankhla Jul 13 '17 at 11:18
  • 3
    children(':selected') doesn't work if you're making use of , you need find('option:selected') – Hill79 Aug 07 '17 at 11:04
274

Try this:

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

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

$("[id*='MyDropDownId'] :selected")
kgiannakakis
  • 96,871
  • 26
  • 155
  • 191
  • The ASP.NET version here is the only Jquery that works with asp.net for me, I hence assent to that one. This one: (**'$("#yourdropdownid option:selected").text();'** did not work in an asp.net page using a master page. – netfed Sep 09 '13 at 16:40
  • 5
    it doesn't work because asp.net master pages throws random characters in front of the selector. It's technically looking for something like `("#ct0001yourdropdownid) ` – CSharper Apr 03 '14 at 17:06
  • http://javascriptstutorial.com/blog/selecting-dropdown-element-using-javascript-or-jquery/ – Dilip Kumar Yadav Oct 18 '16 at 12:03
  • 1
    @CSharper - I think saying ASP.NET *throws random characters* is rather misleading. They're not random at all, they're structural and follow strict rules (based on things like whether you put an `ID` on your control, and if not then based on the index of where they occur in the current level of the tree, etc) – freefaller Mar 09 '17 at 14:03
  • Hi how do you do this inside a Table row/cell, how do you know which of the Dropdown from the table, especially in ASP MVC 5 – Transformer Mar 12 '17 at 07:41
  • You can also do $("#''"), but the selectors are cleaner. If you had a lot of controls and you were in a looping scenario then it might benefit you to use the id, but I've never had a performance issue using the selector method. – Charles Byrne Nov 28 '17 at 13:40
221

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
  • 28,342
  • 21
  • 95
  • 123
JYX
  • 2,453
  • 1
  • 15
  • 14
  • 7
    Don't be hating on this answer. Using the "find" keyword did the trick for me. I was coming from a completely different context. – Gaff Aug 06 '12 at 13:50
  • 5
    you don't need the option part at all as its implied with selected.. simply using $('#yourdropdownid :selected').text(); will work fine – Dss Jan 17 '13 at 18:52
  • jquery-1.10.2.min, this one worked for me, not others. – kubilay Feb 27 '14 at 07:18
  • The popular answer works, but I needed it on an already obtained reference to the select, so this is the best answer for me – Graham Oct 22 '18 at 09:53
109

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,119
  • 2
  • 25
  • 44
Kirk Liemohn
  • 7,203
  • 7
  • 43
  • 50
72

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,070
  • 1
  • 16
  • 17
66
$("option:selected", $("#TipoRecorde")).text()
Muhammad Usman
  • 11,962
  • 6
  • 34
  • 56
Rafael
  • 661
  • 5
  • 2
55

This works for me:

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

jQuery version: 1.9.1

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Binita Bharati
  • 2,998
  • 28
  • 20
55

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

Aziz Shaikh
  • 15,104
  • 9
  • 55
  • 73
Neeraj
  • 687
  • 5
  • 2
  • 38
    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
43

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,096
  • 8
  • 21
  • 44
Kamrul
  • 6,641
  • 3
  • 26
  • 29
35

Various ways

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

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

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

4. $("#myselect").find(":selected").text();
MaxEcho
  • 13,325
  • 6
  • 72
  • 84
32

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
  • 6,993
  • 2
  • 41
  • 53
31
$("#dropdownID").change(function(){
  alert($('option:selected', $(this)).text());
});
124
  • 2,509
  • 22
  • 36
29
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,426
  • 14
  • 44
  • 65
Zarni
  • 307
  • 3
  • 2
19

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
  • 28,342
  • 21
  • 95
  • 123
FAA
  • 489
  • 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
  • 28,342
  • 21
  • 95
  • 123
Nikhil Agrawal
  • 23,885
  • 19
  • 85
  • 117
15
$("select[id=yourDropdownid] option:selected").text()

This works fine

Thangamani Palanisamy
  • 4,684
  • 3
  • 29
  • 36
13

For getting selected value use

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

and for getting selected item text use this line:

$("#dropDownId option:selected").text();
Mojtaba
  • 464
  • 9
  • 17
12
$('#id').find('option:selected').text();
Nikul
  • 1,005
  • 1
  • 11
  • 29
11

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,096
  • 8
  • 21
  • 44
Sandeep Shekhawat
  • 627
  • 1
  • 8
  • 17
11

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
  • 988
  • 11
  • 20
10
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
Kalaivani M
  • 1,103
  • 13
  • 28
  • I tried logging the div in `var div = e.options[e.selectedIndex].text;`, and it only displays the first option item. How to get each item to display? – Chris22 Aug 09 '17 at 06:59
  • @Chris22 refer this link https://stackoverflow.com/questions/3923858/jquery-getting-all-dropdown-values – Kalaivani M Aug 30 '17 at 17:00
10

Simply try the following code.

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

it returns the text of the selected option.

Muddasir23
  • 485
  • 4
  • 14
9

Use:

('#yourdropdownid').find(':selected').text();
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
kishore
  • 1,432
  • 5
  • 20
  • 31
8

the following worked for me:

$.trim($('#dropdownId option:selected').html())
Mohammad Dayyan
  • 18,338
  • 35
  • 143
  • 207
  • 1
    Note: Do not use this for multi select. It only grabs the first selected value. – max Mar 28 '16 at 04:25
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,096
  • 8
  • 21
  • 44
vineet
  • 10,659
  • 9
  • 50
  • 69
7

This work for me:

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

I'm using jQuery 1.10.2

7

$(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,340
  • 15
  • 53
  • 93
Bhanu Pratap
  • 1,225
  • 12
  • 14
  • Hi how do you do this **inside a Table row/cell**, *how do you know which of the Dropdown from the table?* I am trying to make an ajax call to fetch and populate values, I can do this outside in a page, but not inside the Table Row... can you add some help please – Transformer Mar 12 '17 at 07:45
5

This code worked for me.

$("#yourdropdownid").children("option").filter(":selected").text();
Naveenbos
  • 2,396
  • 3
  • 31
  • 58
4

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

x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})
max
  • 7,861
  • 15
  • 70
  • 115
3
$("#dropdown").find(":selected").text();


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

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

$("#dropdown").children(":selected").text();
shyamm
  • 400
  • 6
  • 14
2

For multi-selects:

$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
Curtis Yallop
  • 5,490
  • 3
  • 37
  • 27
2

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
  • 53,729
  • 20
  • 259
  • 241
1
$("#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
  • 824
  • 1
  • 12
  • 26
0

Just add the below line

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

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

Nirav Vasoya
  • 358
  • 3
  • 18