Sahin Yanlık Sahin Yanlık - 3 months ago 10
jQuery Question

Select2 can't set multiple value

I am trying to show selected values in select2-jquery component.

var select = $(".select2").select2({
multiple: true,
placeholder: "",
width:'100%',
data: z
});
var selectedValues = $("#sourceValues").val().split(',');

$.each( selectedValues, function(k,v){
$(".select2").select2('val',v);
})


sourceValues holds the value e.g : 2,4
And z is array of object that holds id and text as suggested.
I can see values that I liked to so but cant't show the selected values.
Also If I try to run the query on chrome console it works , if I write something like ;

$(".select2").select2('val',4)


It founds the id number for and select it .

Answer

http://ivaynberg.github.io/select2/#documentation

val

Attached to select - Multi-Valued - Array of the value attributes of the options that should be selected. null for empty.

So:

var selectedValues = $("#sourceValues").val().split(',');
$(".select2").select2('val',selectedValues);
Comments