I need to run several select and store their value
in an array with jQuery, but I can not. How can I do this?
I need to run several select and store their value
in an array with jQuery, but I can not. How can I do this?
You can create a collection with $('select')
and then use .map()
to generate an array, and .get()
to convert from a jQuery collection to a native array.
An example would look like this:
var values = $('select').get().map(function(select){
return select.value;
});
I do not quite understand what you want but see if it helps:
var array = new Array();
$('select').each(function(index, el) {
array.push($(el).val());
});
console.log(array);
If you want to get only the selected options:
var array = new Array();
$('select option:selected').each(function(index, el) {
array.push($(el).val());
});
console.log(array);