Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent select2 from taking user input as one of the tag suggestions

Implemented Select2 in my project. Using the tag feature provided in the plugin. The feature works as suggested in the documentation, however I am stuck at one point.

I have initialized the tag as follows:

HTML

<div class="form-group">
       <label for="languages">Languages <span class="mandatory">*</span> :</label>
       <input type="text" required="" placeholder="Languages" name="languages" id="languages" class="form-control">
</div>

JS:

$('#languages').select2({
     tags: languagenames,
});

lanugagenames data:

[{"id":"1","text":"Afrikaans"},{"id":"2","text":"Albanian"},{"id":"3","text":"Arabic",....}]

On entering characters in the input box, I get suggestions in the drop-down, however I am also getting the entered characters as one of the suggestions (screen attached). For example, while trying to enter english, I enter "En" and it is present as one of the suggestions. I checked the data set and there is not entry for "En". I cannot find any particular configuration to stop this extra entry. Any suggestion on how I can prevent this extra entry from appearing?

enter image description here

like image 899
Kumari Manisha Avatar asked Sep 06 '14 11:09

Kumari Manisha


1 Answers

I didnt know this plugin before, but it looks great.

Try something like this:

$('#languages').select2({
     tags: languagenames,
     createSearchChoice : function(term){
        return false;
    }
});
like image 198
reyaner Avatar answered Oct 18 '22 08:10

reyaner