Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript select2 allowed tags

I need to allow users select ONLY allowed tags in select.

Currently I have that:

$("input#id_txtcolor").select2({tags:["red", "green", "blue"]});

Can you help me please with that?

like image 853
nvvetal Avatar asked Feb 07 '13 16:02

nvvetal


People also ask

What are tags in Select2?

From the documentation of the select2 plugin one can read: In addition to a pre-populated 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.

How do I use Select2 tags?

Automatic tokenization into tags Select2 supports ability to add choices automatically as the user is typing into the search field. Try typing in the search field below and entering a space or a comma. The separators that should be used when tokenizing can be specified using the tokenSeparators options.

How do I add options to 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).


1 Answers

as of 3.3 you can specify your own createSearchChoice when using tags that will always return null, thus preventing the default choice from being created.

$().select2({
    createSearchChoice: function() { return null; },
    tags:...
});
like image 56
igor.vaynberg Avatar answered Oct 21 '22 15:10

igor.vaynberg