Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable creating new tags with select2 v4.0?

I've been trying out the new Select2 v4.0 which has a lot of improvements. I'm mainly interested in the tags feature. I want to be able to search for tags via ajax and only be able to select a tag from the shown results and not be able to create new tags. The functionality is similar to StackOverflow - if you don't have the necessary reputation you can't create new tags, but you can still tag a question with existing tags.

Here's a jsfiddle with my code which is taken from the examples. In the example, you can create new tags which are what I want to limit. The user should be able to select tags only from the list that's retrieved from GitHub via ajax.

Does anybody know how to disable this functionality?

like image 860
tftd Avatar asked May 27 '15 00:05

tftd


People also ask

How do I make Select2 disabled?

$('select'). select2('enable',false); This works for me.

How do you add new option if not exist in the list using Select2?

New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).

How do I hide Select2 search box?

In order to hide the search box in select2, you can hide the search box by setting "minimumResultsForSearch" to a negative value. $('select'). select2({ minimumResultsForSearch: -1 });

What is Select2 tag?

In addition to a prepopulated menu of options, Select2 can dynamically create new options from text input by the user in the search box. This feature is called "tagging". To enable tagging, set the tags option to true : orange. white.


1 Answers

This should work - in the initialization of select2, try returning undefined from the createTag function like so:

createTag: function(params) {                 return undefined;            } 
like image 164
SnowJon Avatar answered Sep 25 '22 13:09

SnowJon