Select2: Select2 Load and Set Multiple Value

Created on 21 Sep 2016  ·  3Comments  ·  Source: select2/select2

hi,
I have some problems with select2 with multiple values.

4.0.3 version

1) I have not put default but he always puts the first element of the select. Why?

$('#test').select2({
multiple:true,
placeholder: 'Select value...'
,tokenSeparators: [','],
});

2) I want to display two values saved on select but sql2 viusalizza me only one!

$('#test').on('change', function () {
$('#hftest').val($(this).val());
});

hftest.value = "123,456";

var selectedValuesTest = $('#hftest"').val().split(',');
$('#test').select2('val',selectedValuesTest);");

Browser Console reporting: The select2('val') method was called on an element that is not using Select2.???

3) in IE11 it does not make you write on the field displaying the available values ​​filtered but only allows you to select from the list. Why?

Thanks for help!

Mauro

Most helpful comment

1) I think you 1st need to apply select2-multiple class in the markup of select2 dropdown ($('#test') in your case) with attribute multiple. :

2) After that bind the select2 in document.ready function :
$('#test').select2({
multiple:true,
placeholder: 'Select value...'
});

3) To display multiple value use:
$('#test').val(selectedValuesTest).trigger("change");

All 3 comments

I don't think you need to call .select2('val', ...) anymore, IIRC, you can just do, in your case, $('#test').val(selectedValuesTest)

1) I think you 1st need to apply select2-multiple class in the markup of select2 dropdown ($('#test') in your case) with attribute multiple. :

2) After that bind the select2 in document.ready function :
$('#test').select2({
multiple:true,
placeholder: 'Select value...'
});

3) To display multiple value use:
$('#test').val(selectedValuesTest).trigger("change");

It's unclear what specific topic this question is asking about, or the specific problem it is referring to. At any rate, the updated docs should cover all of these points now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanderhahn picture sanderhahn  ·  4Comments

e-frank picture e-frank  ·  3Comments

FezVrasta picture FezVrasta  ·  3Comments

ghost picture ghost  ·  3Comments

fabienheureux picture fabienheureux  ·  3Comments