HaReL HaReL - 9 months ago 40
Javascript Question

Select2 - loading tags but stay with the ability to add new

I'm trying to use the tags in Select2. I want to give the user the ability to load some known tags and sometimes to add new, if he wants.

The problem is that Select2 give me the option to add new tags only in new SELECT field. If your'e preloading some data to the field and pre-selecting it - you are losing that ability.

Here there is an example of the problem:

multiple: true,
tags: true,
placeholder: "enter",
tokenSeparators: [','],
data: ['11']
//The problem is in the next line

In the current state new tags are accepted. If you will remove the // in the last line you won't be able to add new tags. Any idea why?


Answer Source

OK, finally I got it.

Actually the select2() function re-initialize the script. But if you are re-sending the tags attribute, you are also re-enabling that ability.

$('#tags').val(['11','22']).select2({tags: true});

Thanks anyway...