Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  159] [ 6]  / answers: 1 / hits: 33741  / 10 Years ago, tue, april 8, 2014, 12:00:00

I'm using select2 and in my site. I need to know at some point if the dropdown is open or closed. I've studied the documentation but I don't see how this can be done. For example, something like this would be nice:



if ($('select').select2('isOpen') === true) { ... }


Any suggestions?


More From » jquery

 Answers
78

By doing some code inspection, it looks like select2-dropdown-open is the class that it adds. But there is an event select2-open in the documentation that fires when the dropdown is open. You can use that to set a variable, or perform an action (also select2-close).



You can do something like this:



$(#e11).on(select2-open, function() { 
$(this).data(open, true);
});
$(#e11).on(select2-close, function() {
$(this).data(open, false);
});

if ($(#e11).data(open)) {
//do something
}


2018 Edit



It appears that the names of the events have been updated since 2014. See user1636505's answer below.


[#71558] Monday, April 7, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janiajohnnad

Total Points: 146
Total Questions: 92
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;