Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 - input field without dropdown

I'm working with select2 and I have a input field where I want to providde search terms. enter image description here

As you can see I have a dropdown where I can select or type my own text.
My jquery code:

function createQuestionTags(data){
  $(".question").select2({
    createSearchChoice: function (term, data) {
        if ($(data).filter(function () {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id: term,
                text: term
            };
        }
    },
    data: data,
    placeholder: "Enter Question",
    allowClear:true
  });
}

How can I set that it is just an input field where you can type and not with a dropdownlist?

like image 344
nielsv Avatar asked Dec 03 '25 12:12

nielsv


1 Answers

A dropdown will always be visible because that's how Select2 works. It's a really good library but this happens to be one of the constraints you will have to work through. The very simple reason behind is:

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results. — Select2

You can try and forcefully hide it using CSS but I haven't tried that.

I recently came across selectize.js which is equally good as Select2 and fulfils your requirements.

like image 128
Aniket Avatar answered Dec 05 '25 02:12

Aniket



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!