Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery / Select2: How to remove blank default dropdown for tags / tokens

I use the jQuery Select2 plugin to add tagging support to an input field
(source: http://ivaynberg.github.io/select2/).

Everything works as intended, my only problem is that this always shows a one-line default dropdown below the input when entering a tag.

I assume this is caused by a functionality that allows you to preset different tags which then can be selected from this dropdown. This is nice if you use it but if you don't need it then it looks confusing.

I tried removing the line tags:[''], from the instantiation but than the plugin doesn't work any more. How can I get rid of this?

My HTML input:

<input type="text" class="form-control sel2Tags" id="tags" name="tags" />

My jQuery initialisation:

$('.sel2Tags').select2({
    tags:[''],
    tokenSeparators: [','],
    multiple: true
});
like image 369
user2571510 Avatar asked Oct 29 '25 16:10

user2571510


1 Answers

You are setting a empty value inside your tags. You should change tags: [''] to tags:[].

With this, you still get a line below the input with the message No matches found.

If you want to remove this line you can "hide" it. According to this issue on GitHub, you can do the following:

<input type="text" class="form-control sel2Tags" id="tags" name="tags" 
    style="width:300px"  />

<style>
    .select2-hidden {
        display:none !important;
    }
</style>

<script>
    $('.sel2Tags').select2({
        tags:[],
        tokenSeparators: [','],
        multiple: true,
        formatNoMatches: function() {
            return '';
        },
        dropdownCssClass: 'select2-hidden'
    });
</script>
like image 149
Luís Cruz Avatar answered Oct 31 '25 12:10

Luís Cruz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!