Search code examples
jquery-select2-4

Select2 only create tag when finished


I am attempting to use Select2 in tag mode. It appears the createTag and insertTag functions are called on every key press when entering a new tag. Is it possible to only create a tag when the user selects the new option/tag (either via mouse or keyboard)? I have some processing to do with the tags as they are entered, but only when the user deems them complete.

$(this.select).select2({
    tags: true,
    createTag: function (params) {
        console.log(params.term);
        return {
            id: params.term,
            text: params.term,
            newTag: true
        }
    },
    insertTag: function (data, tag) {
        console.log(tag.text);
        data.push(tag);
    }
});

This logs every single keystroke.


Solution

  • Turns out it's the simple change event which fires when the user has finished entering a tag - listening to that allowed me to do the required processing before the option was selected.