janhartmann
janhartmann

Reputation: 15003

Get values from multiple selections in SELECT

I have a selection box that allows you to select multiple options. I need to access all the selected values with JavaScript - possible a array of values?

Upvotes: 3

Views: 5737

Answers (3)

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

var values = [];
$('#my_select option:selected').each(function(i, selected){
    values[i] = $(selected).attr('value');
});

Upvotes: 3

karim79
karim79

Reputation: 342625

I would use $.map:

var values = $('#mySelect option:selected').map(function() {
    return this.value; // or $(this).val()
}).get();

Upvotes: 1

Doug Neiner
Doug Neiner

Reputation: 66191

This is the best way to get an array of the selected values back:

$("#mySelect").val(); // Return an array of the selected options values

This assumes that multiple="mutliple" and size is greater than one on the select element.

Upvotes: 4

Related Questions