Select2 CDN JS Complete Ajax Example in 3 Simple Steps

A step-by-step guide that provides 3 easy steps to easily set up and render a working jQuery Ajax Select2 dropdown list using CDN JS and CSS files

Select2 CDN JS Complete Ajax Example in 3 Simple Steps

The following topics will be discussed in this post.

In this post, we will build a full example of an Ajax dropdown list using Select2 JavaScript CDN files.

The dropdown list loads its data by Ajax from a REST API, then displays the returned data in the dropdown menu.

The Select2 dropdown will have the following features:

  1. Load an initial list of users returned from a REST API.
  2. Search through those users and display the filtered data.
  3. Allows multiple selections and displays the selected users.

This example using only HTML, JavaScript, and CSS end to end. The REST API used is already live and open for anonymous access to anyone.

Thanks to GO Rest for maintaining such an awesome live fake users API. No programming language experience is required to understand this example.

Interactive Running Demo

You will get a result similar to the below working demo. Feel free to try it and interact with it yourself.

Step 1: Setting up required CSS CDN files

In this example, only Bootstrap 4 and Select2 CSS libraries are required.

To set up the required CDN CSS files, put the following references in the HTML <head> tag of your page.

Bootstrap 4 CSS CDN on JSDelivr:

 <link rel="stylesheet" href="" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> 
Select2CSS CDN on JSDelivr:

<link rel="stylesheet" href="" 
Now, we only need jQuery, Popper, Bootstrap 4 and Select2 JS libraries to run this example.

To set up the required CDN JS files, put the following reference at the bottom of your HTML tag of your page.

jQuery CDN JS file:
<script src="" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

Popper CDN JS file on JSDelivr:

<script src="" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>

Bootstrap 4 CDN JS file on JSDelivr:

<script src="" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script>

Select2 CDN JS file on JSDelivr:

<script type="text/javascript" src=""></script>
Once you place the required CSS and JS CDN references, you are ready to start building your nice Select2 dynamic dropdown list with Ajax.

Step 2: Putting the required HTML in place

Your HTML code required to run this example should be as simple as the following:

<select id="UserList" name="UserList" multiple=""></select> 

The “multiple” attribute is needed here to let Select2 know that this is going to be a dropdown list that allows multiple selection.

Step 3: Configuring the AJAX Select2 dropdown list with JavaScript

In this example, we are going to configure the Select2 dropdown list to call a fake random user public REST API at:

A GET request to that API will return a response in the following JSON format:

JSON Response of the Public Users Fake REST API

Now, at the bottom of your HTML tag, add a <script> tag as follows:

Notice the following important configurations: 

  1. delay: This is a rate limit duration in milliseconds that will let the dropdown wait after the user finishes typing until the next Ajax request is sent.
  2. data: This is a JavaScript function that is used to send some parameters to the requested API in a JSON object format. In this case the dropdown will send 2 parameters to the API, name, and page.
    • name: is used to pass whatever term the user types in the dropdown to search for users.
    • page: is used to pass the next page number to the API as long as the user is scrolling through the items in the dropdown list and the API has more data to return.
  3. processResults: It is a JavaScript function that is used to identify which data in the returned response represents the pagination and which represents list items. In this case we only need the total number of items from the response, which is found at
    • more: used to tell the dropdown if there are more items left in the API to return or not.
  4. templateResult: This is the JavaScript function responsible for rendering the UI of each list item in the dropdown menu. In this example, this function was used to render each user’s initials, full name and the status icon (active or inactive based on the returned data from the API).
The following screenshot is an example of what you should see on your page after finishing the previous 4 steps.

jQuery Select2 Ajax Dynamic Dropdown List


In this example, we could easily build a fully functioning Ajax Select2 dropdown list with CDN CSS and JS files.

We also attached a fully working HTML page in the next attachments section that you can download, run, and see the code for yourself.

If you have any questions, or feedback, feel free to leave me a comment in the comments section and I will answer as soon as possible.
Select 2 Demo HTML Page - Working
Fady Soliman
Fady SolimanMember since
Jun 22, 2013
Did you find this helpful?
Leave your Comment