Dan
Dan

Reputation: 19

select2 select multiple itmes from javascript

var selectedValues = new Array();
selectedValues[0] = "1";
selectedValues[1] = "4";
$('#select2-multiple-employees').select2('val', selectedValues);

It selects only the first index in the array of selectedValues. I'm using select2 version: 4.0.8

Is there another way to do this? It doesn't work for me

Solution: Adding under insert selectedValues

$('#select2-multiple-employees').trigger('change');

Upvotes: 0

Views: 58

Answers (1)

Mr.
Mr.

Reputation: 10122

here are 2 excerpts from select2 documentation:

Configuration: To configure custom options when you initialize Select2, simply pass an object in your call to .select2()

  • but you do not pass such object

Basic Usage: The DOM cannot be safely manipulated until it is "ready". To make sure that your DOM is ready before the browser initializes the Select2 control, wrap your code in a $(document).ready() block.

  • but you do not wait for the page to be ready

i hope this will give you some lead.

Upvotes: 1

Related Questions