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?
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?
Use the each function to iterate over them and push the values into an array.
var selected = [];
$('#uniquedropdown').each( function() {
selected.push( $(this).val() );
});
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