1

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. ?

3
  • 1
    do you have 1 drop down that allows 1 value to be selected? multiple values? multiple drop downs? Commented Jul 13, 2012 at 18:50
  • The issue you're experiencing is that the selector is returning multiple elements. In your case the id variable now is a list of selected options that you have to loop through. Any of the answers below that use .each() will give you what you need. Commented Jul 13, 2012 at 18:52
  • Are you interested in which ID's have which values or are are you dealing with the case where you have multiple select elements that they can pick items from and it becomes a set? Commented Jul 13, 2012 at 18:53

4 Answers 4

3

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)
})
Sign up to request clarification or add additional context in comments.

Comments

2

Try like this:

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

and you will have the selected values of all dropdowns.

2 Comments

Love this approach. Perhaps do "select[id*='drpCategory']" since they *= selector is going to be costly.
thanks for your answer. i tried but nothing change. When i selected the 3. item (third item value is 22), The Value is always come dropdowns's first item value (first item value is 2) :s
0
$("#input_id").click(function(){
 var id = $("#drpCategory option:selected").val();
})    

Comments

-1

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

1 Comment

please explain your downvotes. the OP specifically said "I have an asp.net DropdownList Control", singular, a single drop down, which this would work fine with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.