Arun
Arun

Reputation: 3731

Select2 plugin multiple selects only first value from array

I am using select2 plugin in my project.

When I dynamically add values to the multiple select as array, it only shows the first value from the array.

HTML

<select multiple id="selId" name="selId[]"  required> 
    <option value="1">A</option>
    <option value="2">B</option>
    <option value="3">C</option>
    <option value="4">D</option>
    <option value="5">E</option>
</select>

Jquery

$(function() {
  $("#selId").select2();
  $("#selId").select2('val', [1, 2]);
})

I want to make select the 1st and 2nd options.

What I am missing?

I created a fiddle explains the same Here

Please give suggestions. Any help could be appreciated

Upvotes: 3

Views: 1370

Answers (2)

Juan.Queiroz
Juan.Queiroz

Reputation: 227

You can use this solution:

$(function() {
  $("#selId").val(["1","2"]).select2();
})

You can see here.

Upvotes: 2

Quentin Roger
Quentin Roger

Reputation: 6538

You can try something like this :

$(function() {
  $("#selId").select2();
  $("#selId").val([1,2]);
  $("#selId").trigger('change');
})

https://jsfiddle.net/otegbdag/

Upvotes: 1

Related Questions