Confirm a select2 selection

Viewed 1130

I'm using select2 v4, and I'm trying to display a confirm() when the user selects an option from the dropdown.

So I have the event .on("select2:select", function(e) {}) and I want to prevent select2 if the users chooses cancel from the confirm dialog.

I have the following:

    $("#select2users").select2().on("select2:select", function(e) { 
        var data = e.params.data;

        if(!confirm("Are you sure you want to ban this user?")) {
            e.preventDefault();
        }
    })

e.preventDefault(); doesn't seem to have any effect here. I've been searching and cannot achieve any decent answer. How should I do it?

1 Answers

Silly of me, I achieved it simply by clearing the value if the user cancel's the confirm dialog.

Like so:

    $("#select2users").select2().on("select2:select", function(e) { 
        var data = e.params.data;

        if(!confirm("Are you sure you want to ban this user?")) {
            $(this).select2('val', '');
        }
    })
Related