select2 multiple selected code example
Example 1: select 2 multiselect
//Installation
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
// In your Javascript (external .js resource or <script> tag)
$(document).ready(function() {
$('.js-example-basic-single').select2();
});
Example 2: select 2 select a value
SELECT2 V4 :
============
For select2 v4 you can append directly an option/s as follow:
<select id="myMultipleSelect2" multiple="" name="myMultipleSelect2[]">
<option value="TheID" selected="selected">The text</option>
</select>
Or with JQuery:
---------------
var $newOption = $("<option selected='selected'></option>").val("TheID")
.text("The text")
$("#myMultipleSelect2").append($newOption).trigger('change');
other example :
---------------
$("#myMultipleSelect2").val(5).trigger('change');