Store a select array in a variable js

1

I need to run several select and store their value in an array with jQuery, but I can not. How can I do this?

    
asked by anonymous 26.08.2017 / 03:03

2 answers

0

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;
});
    
26.08.2017 / 07:43
0

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);
    
26.08.2017 / 05:14