Copy+Paste three mail addresses (JSFiddle included)

I want to copy&paste several mail addresses.

If I copy&paste this into the fiddle, then the last address (three-at-e–com) does not get a new tag.

https://jsfiddle.net/guettli/c8od413n/

Text to copy&paste: one-at-e–com two-at-e–com three-at-e–com

I know that I have to press space twice to make three-at-e–com a new tag. But this is strange for new users.

I guess select2 does not distinguish between typing in the string and copy&paste up to now.

Is there a work-around?

PS: I needed to hide the mail addresses, otherwise the forum software says that new users can’t make more than two links into a post :frowning:

I don’t think this is a bug. The Select2 widget doesn’t have any special logic to handle text pasted into its search field. It’s operating exactly the same way it would if the user typed two(at)e.com{space}three(at)e.com.

Instead of pressing space twice to make three(at)e.com a tag, you can press Enter.

I suppose if you wanted to make this work automatically, you could attach a paste event handler to the Select2’s search field. It is a regular text input element with a class of select2-search__field. In your event handler you could programmatically update the value of the pasted text to add a space at the end. This should trigger the Select2 widget to convert the last value to a tag. There is an example of how to do this in the MDN documentation for the paste event I linked above.

1 Like

Thank you very much for your answer. I will try to solve this in my JS code.

On the other hand: Wouldn’t it be nice, if select2 could handle copy+paste out of the box?

Yes, it would. You could submit it as a feature request on the Select2 GitHub.

I created a feature request here: https://github.com/select2/select2/issues/5338