jquery-select2 Getting started with jquery-select2

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

This section provides an overview of what jquery-select2 is, and why a developer might want to use it.

It should also mention any large subjects within jquery-select2, and link out to the related topics. Since the Documentation for jquery-select2 is new, you may need to create initial versions of those related topics.

Versions

VersionRelease Notes LinkRelease Date
4.0.3 (Current)Select2 4.0.32016-05-27
4.0.2Select2 4.0.22016-03-09
4.0.2 RC1Select2 4.0.2-rc12016-02-14
4.0.1Select2 4.0.12015-11-27
4.0.1 RC1Select2 4.0.1-rc12015-11-10
3.5.4Select2 3.5.42015-08-30
3.5.3Select2 3.5.32015-08-20
** Older versions can be found at:select2/releases2015-08-20

Jquery - Select2 Installation and Setup

You can include/install Select2 in one of the two ways

  1. Directly using CDN's in your project under the head section of your project.

link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"/>
 
  1. Download the code to your local machine and include it into your project. it should look like this enter image description here

Then include these lines into head section of your html page

  <link href="select2.min.css" rel="stylesheet" />
  <script src="select2.min.js"></script>
 

Note: You need to have Jquery included into your project for Select2 to work correctly.

PITFALL: if Current version of jquery and Select2 are conflicting or Select2 features are not working. Then move the select2 variables and implementations in separate block of document body $(document).ready(function () { //your select2 code ..... });

How to use it: You can define a <select> as follows:

<select id="select2_example">
    <option>Test</option>
</select>
 

Approach1:

      var _mSelect2 = $("#select_example").select2();
 

Approach2:

  <script type="text/javascript">
      $('select').select2();
  </script>
 

Please note that using this approach all the defined 'select' control of page will inherit features of Select2. If you only want to use Select2 for certain controls then skip this step and write your own code to use select2 feature directly and jquery/javascript feature separately for other select control. However, you can use the value of manipulated through Select2 for Jquery or vice-versa.

Further examples of 'The Basics' can be found in the Select2 GIT documentation here

Select2 GIT project is here. Please go through the Select2 Github site for detail feature of this product.

To clear the selected elements of Select2 dropdown.

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function.

<select id="select2_example">
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
 

The dropdown can be reset using Jquery.

$("#select2_example").empty();
 

It can also be reset as below.

$("#select2_example").select2("val", "");
 


Got any jquery-select2 Question?