select2 display default option (static) and do ajax search if not found
<p>I have select2 v4 with a static select (100 options). 
When the user input is not found in the default options list, I need to do an ajax search. </p>

<p>As I have not been able to use the same select object, I use a different for the ajax search. 
So I hide the default select and replace with the ajax_select.
Do the ajax search.  </p>

<p>code:</p>

<pre><code>&lt;script type="text/javascript"&gt;

    var s2_search =''; //global var for search string

    $("#default_select").select2({
        placeholder: "Select an item",
        debug: true,
        minimumInputLength: 0, // important!!! =&gt; without setting to 0 the default data will not be shown

        tags:true,
        createTag: function (params) {
            s2_search = params.term; //assing search string to var
        },

        templateResult: function(data){

            var $result = $("&lt;span&gt;&lt;/span&gt;");

            $result.text(data.text);

            return $result;
        },
        escapeMarkup: function (markup) {
            if(markup == 'No results found'){
               // item is not found in default activate the ajax select
                $('#div_default').addClass('hidden');
                $('#div_new').removeClass('hidden');
                new_select_func(s2_search)
            }
            else {
                return markup;
            }
        }
    });

    var $myselect = $('#new_select').select2({
        minimumInputLength: 0,
        ajax: {
            url: BASE_URL + '/get_more',
            dataType: 'json',
            cache: true,
            delay: 350,
            data: function (params) {
                return {
                    q: params.term//, // search term
                };
            },
            processResults: function (data, params) {
                if(data === false){
                    // if the item is not found in the database display a modal form to insert the item
                    // This works 
                    $.get(BASE_URL+'/get_new_form',{plate: params.term}, function (data) {
                        $('#myModal').find('.modal-body').html(data);
                    });
                    $('#myModal').modal('show');
                }
                else {
                    return {
                        results: data 
                    };
                }
            }
        },
        templateSelection: formatNewSelection 

    });

    function formatNewSelection(data, container) {

        // append the new item to the default select
        $("#default_select").append($('&lt;option&gt;', {value: data.id, text: data.text}));
        //restore default select 
        $('#div_default').removeClass('hidden');
        // hide new select
        $('#div_new').addClass('hidden');

        return data.text;
    }
    function new_select_func(search_val) {
        //close default select 
        $("#default_select").select2("close");
        //open new select 
        $myselect.select2('open');
        // set search string and trigger ajax search
        var $search = $myselect.data('select2').dropdown.$search || $myselect.data('select2').selection.$search;
        $search.val(search_val);
        $search.trigger('keyup');

    }
&lt;/script&gt;
</code></pre>

<p>Definitely not the best way to do it, but I only have one issue. When I select an item in the ajax_select to append it to default select it create the same option 4 times. 
Can anyone tell me why and is there a better way to do it?</p>

<p>Thank you,
Salain</p>