HaReL HaReL - 1 month ago 7
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:
jsfiddle

$("#tags").select2({
multiple: true,
tags: true,
placeholder: "enter",
tokenSeparators: [','],
data: ['11']
});
//The problem is in the next line
//$('#tags').val(['11','22']).select2();


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?

Thanks!

Answer

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...