javascript select2 allowed tags
Asked Answered
N

2

16

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?

Nuclide answered 7/2, 2013 at 16:41 Comment(1)
from the documentation: "Note that when tagging is enabled the user can select from pre-existing tags or create a new tag by picking the first choice which is what the user has typed into the search box so far." So i guess you might be out of luck... but have you tried setting maximumInputLength to 0, that might prevent the user from entering new tags?Colossus
L
31

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:...
});
Lowspirited answered 11/2, 2013 at 0:56 Comment(3)
@reverbnation answer is better, this technically works but is more of a hack, then using the correct behaviourWeisshorn
@Stefan, yep, there's no reason to do it this way when you can just make it a multiple select input.Ungrateful
This is a better aproach to disable user custom tags if you are using Yii Framework - Yiiwheels - WhSelect2. As you are prefilling the 'data' property. Thank you.Hydrophyte
B
16

Sometimes we developer don't use our 7th sense that is common sense also happened with me struggled for 2 hours then it clicked my sense.. if you want user to select only allowed values then its no more tagging its selecting So just remove tags:true option and keep mulitple attribute in select, provide options directly in select element or load remote data.

Britannic answered 18/2, 2015 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.