Arun Arun - 3 months ago 13
jQuery Question

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

Answer

You can try something like this :

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

https://jsfiddle.net/otegbdag/

Comments