Blur Select2 after closing

I want the select2 element to lose focus when the select2-close event fires, which I still tried:

.on("select2-close", function (e) {
    var $focused = $(':focus');
    $focused.blur();
});

and some variations to get a focused element like document.activeElement$ (e.target) not working.

Jsfiddle

+4
source share
1 answer

You need to remove the class .select2-container-activefrom the containing separator:

.on("select2-close", function () {
    setTimeout(function() {
        $('.select2-container-active').removeClass('select2-container-active');
        $(':focus').blur();
    }, 1);
});

I used setTimeouthere as a hacker way to make sure that this trigger after the plugin itself completes the closure.

JSFiddle demo .

+9
source

All Articles