ChatGPT解决这个技术问题 Extra ChatGPT

Get selected text from a drop-down list (select box) using jQuery

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

Just my two cents: An "ASP" drop-down is no special; it's just good old HTML. :-)
One can refer this article: javascriptstutorial.com/blog/…
for vanilla javascript way, see stackoverflow.com/a/5947/32453
Just $(this).prop('selected', true); replaced .att to .prop it worked for all browsers
$("#yourdropdownid option:selected").text();

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

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.
I second the comment about is() returning a boolen; alternatively, use the following small alteration: $('#yourdropdownid').children("option:selected").text();
$('select').children(':selected') is the fastest way: jsperf.com/get-selected-option-text
And if have scape caracteres like \n \t you can add .trim() getting like this: $("#yourdropdownid option:selected").text().trim();
k
kgiannakakis

Try this:

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

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

$("[id*='MyDropDownId'] :selected")

P
Peter Mortensen

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.


N
Nouman Bhatti

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


P
Prabhagaran

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());
});

M
Muhammad Usman
$("option:selected", $("#TipoRecorde")).text()

P
Peter Mortensen

This works for me:

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

jQuery version: 1.9.1


A
Aziz Shaikh

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


Not exactly the answer to the question, but was useful for me. The question wants the selected text.
M
Milap

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();

J
Justin Lessard

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.


M
MaxEcho

Various ways

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

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

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

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

1
124
$("#dropdownID").change(function(){
  alert($('option:selected', $(this)).text());
});

V
Vivek
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.


P
Peter Mortensen

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();

P
Peter Mortensen
 $("#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.


T
Thangamani Palanisamy
$("select[id=yourDropdownid] option:selected").text()

This works fine


N
Nikul
$('#id').find('option:selected').text();

M
Mojtaba

For getting selected value use

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

and for getting selected item text use this line:

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

M
Milap

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
})

N
Naveenbos

This code worked for me.

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

Works too! And if have scape caracteres like \n \t you can add .trim() getting like this: $("#yourdropdownid").children("option").filter(":selected").text().trim();
V
Vishal Thakur

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:

http://api.jquery.com/val/

http://api.jquery.com/text/


M
Muddasir23

Simply try the following code.

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

it returns the text of the selected option.


K
Kalaivani M
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;

P
Peter Mortensen

Use:

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

M
Mohammad Dayyan

the following worked for me:

$.trim($('#dropdownId option:selected').html())

M
Mhandzkie

This work for me:

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

I'm using jQuery 1.10.2


M
Milap

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()

s
shyamm
$("#dropdown").find(":selected").text();


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

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

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

S
Satinder singh

$(function () { alert('.val() = ' + $('#selectnumber').val() + ' AND html() = ' + $('#selectnumber option:selected').html() + ' AND .text() = ' + $('#selectnumber option:selected').text()); });

https://i.stack.imgur.com/gjUBz.png


m
max

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

x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})