-2

I am using the select2 plugin which allows me to set a default value for a multiple select as follows:

$('#myID').select2('val', ['val1','val2','val3',...]);

This works fine so far but when I try to use a variable instead of hard-coding the different vals then it fails so I guess I am passing the variable the wrong way. Can someone here help me with this ?

I tried:

var myVar = "['val1','val2','val3']";
$('#myID').select2('val', myVar);

Many thanks in advance for any help with this, Tim.

user2571510
  • 11,167
  • 39
  • 92
  • 138
  • 2
    Why did you add extra quotes around the value of `myVar`? That's a change from your original code, after which it stopped working. So what is likely to be the reason? – Jon Mar 04 '14 at 11:35
  • Like Jon said, you need to pass array, not string – A. Wolff Mar 04 '14 at 11:35

1 Answers1

2

Try unquoting the array

var myVar = ['val1','val2','val3'];
$('#myID').select2('val', myVar);
Anton
  • 32,245
  • 5
  • 44
  • 54