Mahdi Mahdi - 8 months ago 58
Javascript Question

Select2 val does not work for integer value

See in the Fiddle

I' m using JQuery select2 like the following


<select style="width:150px" id="lang" multiple >
<option value="1">1</option>
<option value="2">2</option>
<option value="11">3</option>


$(document).ready(function() {
placeholder: 'please type'}

my problem is: I want to select an item programmatically

I use following code:


but the above code select the first item not the last one.


I believe...


...initializes Select2, rather than making a selection.

What you're probably looking for is something like this:


If you want to select multiple values, you can use a string array, like so:

//Select 11, 12, 13
$("#lang").val(["11", "12", "13"]).trigger("change");

For more information, check out the Select2 Examples documentation.