close select2 dropdown via javascript/jquery
Asked Answered
O

7

20

Good day, How can I properly close a select2 dropdown via jquery or javascript??

for now Im using select2-dropdown.toggle() to close it,

but I noticed that It will simply hide the list and the select2 box is still being highlighted enter image description here

I want to lost focus it or something like that just to close it properly and be able to come up with a result like this one enter image description here.

by the way the screen shots are dark because those select2 boxes are under a bootstrap modal that would come up whenever I press enter.

Any advice would really be appreciated! Thanks in advance

Orthopedic answered 8/2, 2014 at 16:1 Comment(2)
Try -> focus on any other element .Tsarevna
thank you @TusharGupta for your suggestion, but I already did that, I did set my textbox on my modal to be focused whenever the modal is shown.Orthopedic
O
9

this one works for me $("#select2-drop-mask").click();

Orthopedic answered 8/2, 2014 at 20:2 Comment(0)
D
39

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");
})
Dunseath answered 19/2, 2015 at 13:9 Comment(0)
T
16

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");
});
Trimming answered 11/11, 2015 at 14:31 Comment(1)
the second method worked for me on BS5, thanksInsecticide
O
9

this one works for me $("#select2-drop-mask").click();

Orthopedic answered 8/2, 2014 at 20:2 Comment(0)
A
9

The select2-dropdown-*mask* didn't work for me, but the below did.

$('#select2-drop').select2('close');
Adaptive answered 3/6, 2016 at 20:38 Comment(5)
@stevemoretz - Is there any way to close them by class? (without needing to use each ID)Righthanded
@BeninCA probably by using $('.id').each(function()){$(this).select2('close');}Streaky
@stevemoretz - tried like this, but no go: $('.select2').each(function(){$(this).select2('close');})Righthanded
Posted a new question instead: #58348127Righthanded
@BeninCA That's why I said probably these stuffs are a bit wired.especially in different versions you get different results.Hope that new question gonna get nice and up to date answersStreaky
I
3

To close all in Bootstrap version 4 and above. Try:

$('body').trigger('mousedown');
Intermigration answered 3/8, 2020 at 6:27 Comment(1)
Thanks @Intermigration $(document.body).trigger('mousedown'); worked for me on BS4Concentrated
L
2

All select2 elements has class select2-hidden-accessible. So we can use this class in selector and close all of them

$('.select2.select2-hidden-accessible').select2('close');

or try to close only one of them with id selector.

Limpkin answered 21/11, 2021 at 11:59 Comment(0)
S
1
select2-dropdown.blur();

I think this is what you're looking for. Here you have an example in JSFiddle created by me just now.

Signac answered 8/2, 2014 at 16:4 Comment(1)
thank your @Signac for your answer, Thats also what I thought, that I should use blur() function, but its still not functioning. Im using the textbox by the way, because you cant use the select2's ajax function on "select"Orthopedic

© 2022 - 2024 — McMap. All rights reserved.