csharpcoder
csharpcoder

Reputation: 11

How can I get dropdown selected value using javascript?

I have an asp.net DropdownList Control. when i use this code

var id = $("[id*='drpCategory'] option:selected").val();

the id is always come first index of dropdown. How can i get other selected values. ?

Upvotes: 1

Views: 651

Answers (4)

Engineer
Engineer

Reputation: 48813

Try like this:

var values = $("select[id*='drpCategory']").map(function(){
    return this.value;
}).get();

and you will have the selected values of all dropdowns.

Upvotes: 2

Ram
Ram

Reputation: 144739

you can loop through the select elements, please note that using attribute selector alone is too slow, try this:

$("select[id*='drpCategory']").each(function(){
   alert(this.value)
})

Upvotes: 3

jbabey
jbabey

Reputation: 46657

jquerys .val() will return the selected value of your drop down, or an array of selected values if the drop down allows multiple.

var selectedItems = $('[id*="drpCategory"]').val();

Docs

Upvotes: -1

lusketeer
lusketeer

Reputation: 1930

$("#input_id").click(function(){
 var id = $("#drpCategory option:selected").val();
})    

Upvotes: 0

Related Questions