Select2 Tutorial

How to initialize the select 2

I hope you have downloaded the source. 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>

Leave a Reply

Your email address will not be published. Required fields are marked *