close select2 dropdown via javascript/jquery
this one works for me $("#select2-drop-mask").click();
In v4.0, none of the other answers worked for me. Using jQuery to select just the mask had no effect. I had to use the id of the select box itself:
$("#mySelectElement").select2("close")
This also worked, but may not be preferred:
$("#mySelectElement").select2().trigger("select2:close");
Documentation
Also, for Bootstrap 3, the hidden.bs.modal
event was too late and the select2 mask would linger for a second during the animation. The hide.bs.modal
worked a little smoother for us:
$('#modalYourModal').on('hide.bs.modal', function () {
//close select2 in case its open
$("#mySelectElement").select2("close");
});
I know this is an old question but in order to do this using the API you would simply do the following:
Select2 API
$("#select2-drop-mask").select2("close");
The question also mentions bootstraps modal dialog which tends to be a reason why people want to close it programmatically.
For anyone's info this is how you do that:
Bootstrap 3
$('#myModal').on('hidden.bs.modal', function () {
$('#select2-drop-mask').select2("close");
})
Bootstrap 2
$('#myModal').on('hidden', function () {
$('#select2-drop-mask').select2("close");
})