Documentation v8.1.1

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

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-close-on-select="false" 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-close-on-select="false" 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>

Bootstrap Modal

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

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 Avatars

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>

Rich Content

Initialize Select2 programmatically and add data-kt-rich-content-iconattribute with the path to the user avatar to create a list with user portraits. Additionally, add data-kt-rich-content-subcontentattribute to include additional sub content elements.
// Format options
const optionFormat = (item) => {
    if (!item.id) {
        return item.text;
    }

    var span = document.createElement('span');
    var template = '';

    template += '<div class="d-flex align-items-center">';
    template += '<img src="' + item.element.getAttribute('data-kt-rich-content-icon') + '" class="rounded-circle h-40px me-3" alt="' + item.text + '"/>';
    template += '<div class="d-flex flex-column">'
    template += '<span class="fs-4 fw-bold lh-1">' + item.text + '</span>';
    template += '<span class="text-muted fs-5">' + item.element.getAttribute('data-kt-rich-content-subcontent') + '</span>';
    template += '</div>';
    template += '</div>';

    span.innerHTML = template;

    return $(span);
}

// Init Select2 --- more info: https://select2.org/
$('#kt_docs_select2_rich_content').select2({
    placeholder: "Select an option",
    minimumResultsForSearch: Infinity,
    templateSelection: optionFormat,
    templateResult: optionFormat
});
<div class="border rounded">
<select id="kt_docs_select2_rich_content" class="form-select form-select-transparent" name="..." data-placeholder="...">
        <option></option>
        <option value="0" data-kt-rich-content-subcontent="..." data-kt-rich-content-icon="assets/media/avatars/300-6.jpg">Option Label 1</option>
        <option value="1" data-kt-rich-content-subcontent="..." data-kt-rich-content-icon="assets/media/avatars/300-6.jpg">Option Label 1</option>
        ...
    </select>
</div>

Floating Labels

Use Select2 with Bootstrap Floating Labels:
<!--begin::Input group-->
<div class="form-floating border rounded mb-7">
    <select class="form-select form-select-transparent" id="kt_docs_select2_floating_labels_1">
        <option></option>
    </select>
    <label>Coin Name</label>
</div>
<!--end::Input group-->

<!--begin::Input group-->
<div class="form-floating form-control-solid-bg rounded">
    <select class="form-select form-select-transparent" id="kt_docs_select2_floating_labels_2" data-allow-clear="true">
        <option></option>
    </select>
    <label for="floatingInputValue">Coin Name</label>
</div>
<!--end::Input group-->
var optionFormat = function(item) {
    if ( !item.id ) {
        return item.text;
    }

    var span = document.createElement('span');
    var template = '';

    template += '<img src="' + item.element.getAttribute('data-kt-select2-image') + '" 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_floating_labels_1').select2({
    placeholder: "Select coin",
    minimumResultsForSearch: Infinity,
    templateSelection: optionFormat,
    templateResult: optionFormat
});

// Init Select2 --- more info: https://select2.org/
$('#kt_docs_select2_floating_labels_2').select2({
    placeholder: "Select coin",
    minimumResultsForSearch: Infinity,
    templateSelection: optionFormat,
    templateResult: optionFormat
});

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>

Demos

Metronic Licenses

License FAQs
Regular License
For single end product used by you or one client
$ 49
Extended License
For single SaaS app with paying users
$ 969
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.