502

How can I get the selected value of a dropdown box using jQuery?
I tried using

var value = $('#dropDownId').val();

and

var value = $('select#dropDownId option:selected').val();

but both return an empty string.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
shyam
  • 6,681
  • 7
  • 28
  • 45
  • 3
    Both of those should work. The problem must be somewhere else (e.g. is that code wrapped in a `$(document).ready(...` block?) – karim79 May 06 '10 at 11:12
  • 5
    `var value = $('#dropDownId:selected').val();` – ant May 06 '10 at 11:12
  • 4
    Nope - `$('#dropDownId').val();` is the most concise way to get the selected value. – karim79 May 06 '10 at 11:13
  • 1
    @shyam you don't need select in this statement since IDs are unique for document, you should use tag name only when refering to classes`select#dropDownId option:selected`, – ant May 06 '10 at 11:13
  • possible duplicate of [How to get the Index of select->option tag](http://stackoverflow.com/questions/1817079/how-to-get-the-index-of-select-option-tag) – Chris Moschini Mar 24 '14 at 08:35
  • Refer this article : http://javascriptstutorial.com/blog/selecting-dropdown-element-using-javascript-or-jquery/ –  May 27 '16 at 12:08
  • One can refer this article: http://javascriptstutorial.com/blog/selecting-dropdown-element-using-javascript-or-jquery/ – Dilip Kumar Yadav Oct 18 '16 at 12:01

31 Answers31

942

For single select dom elements, to get the currently selected value:

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

To get the currently selected text:

$('#dropDownId :selected').text();
Peter McG
  • 18,857
  • 8
  • 45
  • 53
62
var value = $('#dropDownId:selected').text()

Should work fine, see this example:

$(document).ready(function(){ 
  $('#button1').click(function(){ 
    alert($('#combo :selected').text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="combo">
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
</select>
<input id="button1" type="button" value="Click!" />
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
Nick Reeve
  • 1,658
  • 2
  • 22
  • 32
  • 5
    "value" is what most coders miss, while declaring elementsand query keeps on returning text instead – Asad Sep 16 '14 at 02:40
27

Try this jQuery,

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

or this javascript,

 var selID=document.getElementById("ddlid");
 var text=selID.options[selID.selectedIndex].text;

If you need to access the value and not the text then try using val() method instead of text().

Check out the below fiddle links.

Demo1 | Demo2

Lucky
  • 16,787
  • 19
  • 117
  • 151
14

try this

$("#yourDropdown option:selected").text();
Taryn
  • 242,637
  • 56
  • 362
  • 405
Kvadiyatar
  • 929
  • 14
  • 29
  • OP is asking for selected value. This answer would get the text content of the drop down. Not wrong, just not what he was asking for. – Joshua Dance May 19 '14 at 20:07
13

I know this is a terribly old post and I should probably be flogged for this pitiful resurrection, but I thought I would share a couple of VERY helpful little JS snippets that I use throughout every application in my arsenal...

If typing out:

$("#selector option:selected").val() // or
$("#selector option:selected").text()

is getting old, try adding these little crumpets to your global *.js file:

function soval(a) {
    return $('option:selected', a).val();
}
function sotext(a) {
    return $('option:selected', a).text();
}

and just write soval("#selector"); or sotext("#selector"); instead! Get even fancier by combining the two and returning an object containing both the value and the text!

function so(a) {
    my.value = $('option:selected', a).val();
    my.text  = $('option:selected', a).text();
    return my;
}

It saves me a ton of precious time, especially on form-heavy applications!

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
9

This will alert the selected value. JQuery Code...

$(document).ready(function () {

        $("#myDropDown").change(function (event) {
            alert("You have Selected  :: "+$(this).val());
        });
    });

HTML Code...

<select id="myDropDown">
        <option>Milk</option>
        <option>Egg</option>
        <option>Bread</option>
        <option>Fruits</option>
    </select>
Uthaiah
  • 1,283
  • 13
  • 14
8

this will do the trick

$('#dropDownId').val();
Singleton
  • 3,701
  • 3
  • 24
  • 37
8

Yet another tested example:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $('#bonus').change(function() {
    alert($("#bonus option:selected").text());
  });  
});
</script>
</head>

<body>
<select id="bonus">
<option value="1">-$5000</option>
<option value="2">-$2500</option>
<option value="3">$0</option>
<option value="4">$1</option>
<option value="5">We really appreciate your work</option>
</select>
</body>
</html>
J Slick
  • 929
  • 11
  • 17
5

Try this, it gets the value:

$('select#myField').find('option:selected').val();

AzNjoE
  • 733
  • 3
  • 9
  • 18
Ângelo Rigo
  • 2,039
  • 9
  • 38
  • 69
4

    function fundrp(){
    var text_value = $("#drpboxid option:selected").text();  
    console.log(text_value);
    var val_text = $("#drpboxid option:selected").val();  
    console.log(val_text);
    var value_text = $("#drpboxid option:selected").attr("value") ;
    console.log(value_text);
    var get_att_value = $("#drpboxid option:selected").attr("id") 
    console.log(get_att_value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<select id="drpboxid">
    <option id="1_one" value="one">one</option>
    <option id="2_two" value="two">two</option>
    <option id="3_three" value="three">three</option>              
</select>
<button id="btndrp" onclick="fundrp()">Tracking Report1234</button>
Michael
  • 657
  • 4
  • 29
3

use

$('#dropDownId').find('option:selected').val()

This should work :)

fubo
  • 44,811
  • 17
  • 103
  • 137
Namila
  • 754
  • 4
  • 8
3

Did you supply your select-element with an id?

<select id='dropDownId'> ...

Your first statement should work!

schaechtele
  • 1,130
  • 1
  • 9
  • 12
3

For selected text use:

value: $('#dropDownId :selected').text();

For selected value use:

value: $('#dropDownId').val();
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
mahi
  • 41
  • 1
2

The id that got generated for your drop down control in the html will be dynamic one. So use the complete id $('ct100_<Your control id>').val(). It will work.

Avi
  • 21,182
  • 26
  • 82
  • 121
VenkeHV
  • 21
  • 2
2

I know this is old but I though I update this with an more up to date answer

$( document ).on( 'change', '#combo', function () {
var prepMin= $("#combo option:selected").val();
 alert(prepMin);
});

I hope this helps

mindmyweb
  • 888
  • 9
  • 13
2

Or if you would try :

$("#foo").find("select[name=bar]").val();

I used It today and It working fine.

SoLiD
  • 31
  • 7
2

To get the jquery value from drop down you just use below function just sent id and get the selected value:

function getValue(id){
    var value = "";
    if($('#'+id)[0]!=undefined && $('#'+id)[0]!=null){
        for(var i=0;i<$('#'+id)[0].length;i++){
            if($('#'+id)[0][i].selected == true){
                if(value != ""){
                    value = value + ", ";
                }
                value = value + $('#'+id)[0][i].innerText;
            }
        }
    }
    return value;
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
2

Try this:

 $('#dropDownId option').filter(':selected').text();     

 $('#dropDownId option').filter(':selected').val();
Mohammad Anini
  • 5,073
  • 4
  • 35
  • 46
saravanajd
  • 181
  • 1
  • 4
2

You can do this by using following code.

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

If you want to get the selected value from the select list`s options. This will do the trick.

$('#dropDownId option:selected').text();
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
1
$("select[id$=dropDownId]").val()
  • try this
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
rakesh
  • 11
  • 2
1

use either of these codes

$('#dropDownId :selected').text();

OR

$('#dropDownId').text();
Pavan
  • 33,316
  • 7
  • 50
  • 76
Bipin
  • 51
  • 3
1
$("#selector <b>></b> option:selected").val()

Or

$("#selector <b>></b> option:selected").text()

Above codes worked well for me

Jishnu V S
  • 8,164
  • 7
  • 27
  • 57
1

This is what works

    var value= $('option:selected', $('#dropDownId')).val();
Shittu Joseph Olugbenga
  • 6,396
  • 5
  • 26
  • 37
0

You can use any of these:

$(document).on('change', 'select#dropDownId', function(){

            var value = $('select#dropDownId option:selected').text();

            //OR

            var value = $(this).val();

});
0

You need to put like this.

$('[id$=dropDownId] option:selected').val();
AzNjoE
  • 733
  • 3
  • 9
  • 18
0

Use the method below to get the selected value on page load:

$(document).ready(function () {
$('#ddlid').on('change', function () {
                var value = $('#ddlid :selected').text();
                alert(value);`
            });
});
RedBassett
  • 3,469
  • 3
  • 32
  • 56
0

For Normal Page loaded dropdowns

  $("#dropDownId").on('change',function(){
     var value=$(this).val();
     alert(value);
  });

for dynamically added options Make sure to keep the id unique

  $(document).on('change','#dropDownId',function(){
    var value=$(this).val();
    alert(value)
  });
Ruzaik Nazeer
  • 474
  • 2
  • 12
  • What if your dropdown option was marked as selected programmatically (like from an ajax request that populates a dropdown)? If you try to get a .val(), you will get null for the value (even though inspecting the source will show you that the option is, indeed, selected)! $("myDD option:selected").val() will yield undefined. – MC9000 Nov 14 '19 at 04:36
  • it is the fastest way to access an element which presents during the page load the $(document).on change will rescan the document once again to find the programmatically added elements I think for his question my answer is much more appropriate. – Ruzaik Nazeer Dec 15 '20 at 09:30
0

If you have more than one dropdown try:

HTML:

<select id="dropdown1" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>
<select id="dropdown2" onchange="myFunction(this)">
    <option value='...'>Option1
    <option value='...'>Option2
</select>

JavaScript:

    function myFunction(sel) {
        var selected = sel.value;
    }
RGriffiths
  • 5,722
  • 18
  • 72
  • 120
0

HTML:

<select class="form-control" id="SecondSelect">
       <option>5<option>
       <option>10<option>
       <option>20<option>
       <option>30<option>
</select>

JavaScript:

var value = $('#SecondSelect')[0].value;
  • Please include an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. – Android Aug 06 '19 at 10:07
0

Using below line of code we can get the dropdownList value .this will work

var getValue = document.getElementById("<%=ddlID.ClientID%>").value;

-1
$("#dropDownId").val('2');
var SelectedValue= $("#dropDownId option:selected").text();
$(".ParentClass .select-value").html(SelectedValue);


<p class="inline-large-label button-height ParentClass" >
     <label for="large-label-2" class="label">Country <span style="color: Red;">*</span><small></small></label>
     <asp:DropDownList runat="server" ID="dropDownId " CssClass="select" Width="200px">          
    </asp:DropDownList>
</p>
Big Smile
  • 1,103
  • 16
  • 30