Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if dropdown is open in Select2?

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?

like image 573
Jeanluca Scaljeri Avatar asked Apr 08 '14 18:04

Jeanluca Scaljeri


People also ask

What select2 dropdown?

By default, Select2 will attach the dropdown to the end of the body and will absolutely position it to appear above or below the selection container. Select2 will display the dropdown above the container if there is not enough space below the container, but there is enough space above it.

What triggers an event Choose 2?

Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform actions. You may also manually trigger these events on a Select2 control using . trigger .


2 Answers

As of Select2 4.0.6, this has been updated to the following

$("#foo").select2("isOpen")

This will return true/false

Hope this helps!

like image 93
MigratingCub Avatar answered Oct 18 '22 20:10

MigratingCub


Select2 4.0 has an isOpen method. If #mySelect is your HTML select element then:

$('#mySelect').data('select2').isOpen()

...will return true or false depending on the state of Select2.

like image 35
Paul Avatar answered Oct 18 '22 20:10

Paul