Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a multiple selected values in dropdown

I am using drop down with multiple select name defined with select[]

How can I get selected values using jquery.

like image 843
Abhi Avatar asked Dec 13 '12 09:12

Abhi


3 Answers

The same way as any form element - use val().

var selectedValues = $("#select").val();

With a multiple select you will see the value as a comma delimited string which can easily be posted for server-side processing or split into an array if required.

Example fiddle

like image 53
Rory McCrossan Avatar answered Nov 02 '22 00:11

Rory McCrossan


If someone wants values with labels. Then here is the solution:

var hexvalues = [];
var labelvalues = [];

$('#myMultiSelect :selected').each(function(i, selectedElement) {
 hexvalues[i] = $(selectedElement).val();
 labelvalues[i] = $(selectedElement).text();
});
like image 26
Narayan Subedi Avatar answered Nov 02 '22 00:11

Narayan Subedi


Try this,

Live Demo

$('#btn').click(function(){
    $('#select option:selected').each(function(){
        alert($(this).text());
    });
})​
like image 29
Adil Avatar answered Nov 01 '22 22:11

Adil