SELECT2 / Javascript select box copy/paste selection like Stackoverflow JS tag at the bottom of create question

154 views Asked by At

I am trying to add a feature to a SELECT2 selectbox. I have a US states in the select2 box. Everything works fine but the users need sometime to enter a list they already had like AZ,MS,KY,WS,TN,HI,FL,NY. I can copy paste them in the select2 box but they are not detected as values like AZ x, MS x, KY x, .... I think I kind of need to have a function to process the box when manual edit but I don't see how to do this. Any ideas ?

1

There are 1 answers

0
mkirouac On

I found in the doc : Automatic tokenization into tags. The trick is to set tags: true properties with one or many token separators.

$(".js-example-tokenizer").select2({ tags: true, tokenSeparators: [',', ' '] })

There is only an issue with the last element if you copy paste many tags at the same time like red,blue,green. I think I need an event on the selectbox to concat a token separator like , or space to the value to have it "tagged".