Tutorial :Get Value of dropdowns with same name using jquery



Question:

I have some dropdowns shown in my asp.net mvc application with the same name (say: 5 dropdowns with the same name 'uniquedropdown'.)

I need to get all the selected values of the dropdowns with the same name using jquery.

How to get it?


Solution:1

You can't use $('select[name="uniquedropdown"]').val() as that will return only the value of the first <select> in the page.

To get an an array of values

var values = $.map($('select[name="uniquedropdown"]'), function (e) {                   return $('option:selected', e).val();               });  

or

var values = $.map($('select[name="uniquedropdown"]'), function (e) {                   return $(e).val();               });  

Here's a Working Demo. add /edit to the URL to see the code


Solution:2

Use the each function to iterate over them and push the values into an array.

 var selected = [];   $('#uniquedropdown').each( function() {       selected.push( $(this).val() );   });  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »