Documentation v8.0.37

Preview Downloads Purchase

Overview

Select2gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options. For full documentation please check the plugin's site.

Usage

Select2's CSS and Javascript files are bundled in the global plugin bundles and globally included in all pages:
<link href="assets/plugins/global/plugins.bundle.css" rel="stylesheet" type="text/css"/>
<script src="assets/plugins/global/plugins.bundle.js"></script>

Initialization

  • Select2's CSS is highly customized in sass/vendors/plugins/_select2.scssSCSS file in order to use it as native component within the design system. The SCSS code is compiled into assets/plugins/global/plugins.bundle.cssand globally included in all pages.
  • Select2's Javascript is globally initialized with some predefined settings in src/js/vendors/plugins/select2.init.jsand the initialization code is bundled within assets/plugins/global/plugins.bundle.jsand globally included in all pages.
  • Select2's is globally initialized through data-control="select2"HTML attribute as defined in src/js/layout/app.jsand some options can be be passed through Select2 Data Attributes.

Basic Examples

Basic examples of Select2 usage with default and solid background input styles:
<select class="form-select" data-control="select2" data-placeholder="Select an option">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select class="form-select form-select-transparent" data-control="select2" data-placeholder="Select an option">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>
Add <option></option>empty option in order to display the placeholder when selection is not made.
Add data-dropdown-parent="#modal_element_id"option to fix uneditable search input when Select2 is used on Bootstrap Modal with tabindex="-1".

Group

Use <optgroup>tag to group select options:
<select class="form-select" data-control="select2" data-placeholder="Select an option">
    <optgroup label="Group 1">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
    </optgroup>
    <optgroup label="Group 2">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
    </optgroup>
</select>

Sizes

Apply the standard Bootstrap input sizes .form-select-smand .form-select-lg:
<select class="form-select form-select-sm form-select-solid" data-control="select2" data-placeholder="Select an option">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select class="form-select form-select-lg form-select-solid" data-control="select2" data-placeholder="Select an option">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

Bootstrap Input Group

Mix and match Select2 with .input-group Bootstrap Input Group  components.
.00
USD
<!--begin::Default example-->
<div class="input-group flex-nowrap">
    <span class="input-group-text"><i class="bi bi-bookmarks-fill fs-4"></i></span>
    <div class="overflow-hidden flex-grow-1">
        <select class="form-select rounded-start-0" data-control="select2" data-placeholder="Select an option">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
    </div>
</div>
<!--end::Default example-->

<!--begin::Solid input group style-->
<div class="input-group input-group-solid flex-nowrap">
    <span class="input-group-text"><i class="bi bi-calendar fs-4"></i></span>
    <div class="overflow-hidden flex-grow-1">
        <select class="form-select form-select-solid rounded-start-0 border-start" data-control="select2" data-placeholder="Select an option">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
    </div>
</div>
<!--end::Solid input group style-->

<!--begin::Both add-ons-->
<div class="input-group flex-nowrap">
    <span class="input-group-text"><i class="bi bi-calculator fs-4"></i></span>
    <div class="overflow-hidden flex-grow-1">
        <select class="form-select rounded-0" data-control="select2" data-placeholder="Select an option">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
    </div>
    <span class="input-group-text">.00</span>
</div>
<!--end::Both add-ons-->

<!--begin::Both add-ons in solid style-->
<div class="input-group input-group-solid flex-nowrap">
    <span class="input-group-text"><i class="bi bi-cash-coin fs-4"></i></span>
    <div class="overflow-hidden flex-grow-1">
        <select class="form-select form-select-solid rounded-0 border-start border-end" data-control="select2" data-placeholder="Select an option">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
    </div>
    <span class="input-group-text">USD</span>
</div>
<!--end::Both add-ons in solid style-->

Clear Selection

Add data-allow-clear="true"attribute to allow clearing selected options.
<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
</select>

Hide Search

Add data-hide-search="true"attribute to hide the search input in the dropdown list.
<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option" data-hide-search="true">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
</select>

Dropdown Width

Use Custom Width Classes  to set dropdown width via data-dropdown-css-class="w-200px"attribute:
<select class="form-select form-select-solid" data-control="select2" data-dropdown-css-class="w-200px" data-placeholder="Select an option" data-hide-search="true">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
</select>

Multiple Selection

Declare a selectbox with multiple="multiple"attribute to allow multiple selection with a tag control.
<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" multiple="multiple">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select class="form-select form-select-sm form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" multiple="multiple">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select class="form-select form-select-lg form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" multiple="multiple">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

Hierarchical Options

Use <optgroup></optgroup>tag to display options in single level groups:
<select class="form-select form-select-solid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true">
    <option></option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
</select>

Modal Example

Use Select2 within Bootstrap Modal:
<!--begin::Modal-->
<div class="modal fade" tabindex="-1" id="kt_modal_1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>

                <!--begin::Close-->
                <div class="btn btn-icon btn-sm btn-active-light-primary ms-2" data-bs-dismiss="modal" aria-label="Close">
                    <span class="svg-icon svg-icon-2x"></span>
                </div>
                <!--end::Close-->
            </div>

            <div class="modal-body">
                <div class="mb-10">
                    <label for="" class="form-label">Modal example</label>
                    <select class="form-select form-select-solid" data-control="select2" data-dropdown-parent="#kt_modal_1" data-placeholder="Select an option" data-allow-clear="true">
                        <option></option>
                        <option value="1">Option 1</option>
                        <option value="2">Option 2</option>
                        <option value="3">Option 3</option>
                        <option value="4">Option 4</option>
                        <option value="5">Option 5</option>
                        <option value="6">Option 6</option>
                        <option value="7">Option 7</option>
                        <option value="8">Option 8</option>
                        <option value="9">Option 9</option>
                        <option value="10">Option 10</option>
                    </select>
                </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-light" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>
<!--end::Modal-->

Countries With Flags Example

Initialize Select2 programmatically and add data-kt-select2-countryattribute with the path to the country flag to create a list with country icons.
// Format options
var optionFormat = function(item) {
    if ( !item.id ) {
        return item.text;
    }

    var span = document.createElement('span');
    var imgUrl = item.element.getAttribute('data-kt-select2-country');
    var template = '';

    template += '<img src="' + imgUrl + '" class="rounded-circle h-20px me-2" alt="image"/>';
    template += item.text;

    span.innerHTML = template;

    return $(span);
}

// Init Select2 --- more info: https://select2.org/
$('#kt_docs_select2_country').select2({
    templateSelection: optionFormat,
    templateResult: optionFormat
});
<div class="form-floating border rounded">
    <select class="form-select form-select-transparent" placeholder="..." id="kt_docs_select2_country">
        <option></option>
        <option value="AF" data-kt-select2-country="assets/media/flags/afghanistan.svg">Afghanistan</option>
        <option value="AX" data-kt-select2-country="assets/media/flags/aland-islands.svg">Aland Islands</option>
        ...
    </select>
    <label for="kt_docs_select2_country">Select a country</label>
</div>

Users With Photos Example

Initialize Select2 programmatically and add data-kt-select2-userattribute with the path to the user avatar to create a list with user portraits.
// Format options
var optionFormat = function(item) {
    if ( !item.id ) {
        return item.text;
    }

    var span = document.createElement('span');
    var imgUrl = item.element.getAttribute('data-kt-select2-user');
    var template = '';

    template += '<img src="' + imgUrl + '" class="rounded-circle h-20px me-2" alt="image"/>';
    template += item.text;

    span.innerHTML = template;

    return $(span);
}

// Init Select2 --- more info: https://select2.org/
$('#kt_docs_select2_country').select2({
    templateSelection: optionFormat,
    templateResult: optionFormat
});
<div class="form-floating border rounded">
    <select class="form-select form-select-transparent" data-placeholder="..." id="kt_docs_select2_users">
        <option></option>
        <option value="0" data-kt-select2-user="assets/media/avatars/300-6.jpg">Emma Smith</option>
        <option value="1" data-kt-select2-user="assets/media/avatars/300-1.jpg">Max Smith</option>
        ...
    </select>
    <label for="kt_docs_select2_users">Select a user</label>
</div>

Validation States

The standard Bootstrap's Form Validation  classes are supported:
Looks good!
Please choose a username.
<form class="was-validated">
    <div class="mb-10">
        <label for="" class="form-label">Valid state</label>
        <select class="form-select form-select-solid is-valid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" multiple="multiple">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
        <div class="valid-feedback">
            Looks good!
        </div>
    </div>

    <div class="0">
        <label for="" class="form-label">Invalid state</label>
        <select class="form-select form-select-solid is-invalid" data-control="select2" data-placeholder="Select an option" data-allow-clear="true" multiple="multiple">
            <option></option>
            <option value="1">Option 1</option>
            <option value="2">Option 2</option>
            <option value="3">Option 3</option>
            <option value="4">Option 4</option>
            <option value="5">Option 5</option>
        </select>
        <div id="validationServerUsernameFeedback" class="invalid-feedback">
            Please choose a username.
        </div>
    </div>
</form>

Explore

Metronic Licenses

License FAQs
Regular License
For single end product used by you or one client
$ 39
Extended License
For single SaaS app with paying users
$ 939
Custom License
Reach us for custom license offers.
Buy Now
Learn & Get Inspired

Support at devs.keenthemes.com

Join our developers community to find answer to your question and help others. FAQs
Get Support
Documentation & Videos
From guides and video tutorials, to live demos and code examples to get started.
Plugins & Components
Check out our 300+ in-house components and customized 3rd-party plugins.
Layout Builder
Build your layout, preview it and export the HTML for server side integration.
Metronic Downloads
Download your prefered framework and demo with one click.
What's New
Latest features and improvements added with our users feedback in mind.
Buy now