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?
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.
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.
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).
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:...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With