jquery select plugin with search code example

Example: jquery select plugin

//place css in head
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" /> 


//Place it before closing body tag above the below code 
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>


//script below should be placed before body closing tag or in seprate script file
<script>
$(document).ready(function() {


    function matchCustom(params, data) {
        // If there are no search terms, return all of the data
        if ($.trim(params.term) === '') {
            return data;
        }

        // Do not display the item if there is no 'text' property
        if (typeof data.text === 'undefined') {
            return null;
        }

        // `params.term` should be the term that is used for searching
        // `data.text` is the text that is displayed for the data object
        if (data.text.indexOf(params.term) > -1) {
            var modifiedData = $.extend({}, data, true);
            modifiedData.text += ' (matched)';

            // You can return modified objects from here
            // This includes matching the `children` how you want in nested data sets
            return modifiedData;
        }

        // Return `null` if the term should not be displayed
        return null;
    }


    $(".search-select").select2({
        matcher: matchCustom
    });
    //$('.search-select').select2();
});
</script>


//Customizing the css are these classes
// This is for input 
.select2-container--default .select2-selection--single {
    background-color: #ebebeb;
    border-radius: 10px;
    padding: 14px 24px 14px 24px;
    border: 1px solid #dcdee1;
    color: #73777fb3;
    min-height: 57px;
    width: 100%;
    font-size: 18px;
}
//This is for arrow position
.select2-selection__arrow {
    top: -7px;
    margin-top: auto;
    margin-bottom: auto;
    bottom: 0;
    right: 15px !important;
}