Select2 Tutorial

How to initialize the select 2

I hope you have downloaded the source code. If you haven’t downloaded them yet then you can visit the select2 official page to download them. The below code initializes the select2 jquery plugin.


<select id="select_option">
<option value="1">Canada</option>
<option value="2">America</option>
<option value="3">China</option>
<option value="4">India</option>
</select>

<script>
$("#select_option").select2();
</script>

Select multiple values on select 2

   
   <select id="select_option">
   <option value="1">Canada</option>
   <option value="2">America</option>
   <option value="3">China</option>
   <option value="4">India</option>
   </select>

<script>
$("#select_option").select2();

var selected_value = ["1", "2", "4"];
$("#select_option").val(selected_value).trigger('change');
</script>

Related Posts: