Select or get all values from Select drop down array using jQuery or JavaScript
html, javascript, jquery, jquery-events
Solution
using map function
var selected = $('select[name="test[]"]').map(function(){
if ($(this).val())
return $(this).val();
}).get();
console.log(selected);
Problem
I have the following HTML code: ``` <Select name=test[]> <option value="">--Please Select--</option> <option value="a">Test Value1</option> </select> <Select name=test[]> <option value="">--Please Select--</option> <option value="a">Test Value2</option> </select> <Select name=test[]> <option value="">--Please Select--</option> <option value="a">Test Value3</option> </select> ``` Now before submitting the form I want to do the following - Check if any of the above is selected. - If selected retrieve its value. I believe I need to loop around but I am unable to find the initial syntax with which I can first grab all the values. I know how to check if any value is selected when 'id' is given for a select element. But in this case I have the 'name' attribute which is an array. I tried looking at different places but I haven't really come across a good solution. Probably I missed something. Please let me know if any further information is required.