I have a situation where I need to dynamically add a class to a div whenever a select2 dropdown is open. To achieve this functionality, I am utilizing the open and close events of select2. The current code successfully adds the class when opening a select2 dropdown and removing it when closing the same dropdown. However, the issue arises when attempting to open another select2 dropdown while one is already open. In this scenario, the close event is triggered instead of keeping the dropdown open. Here is the existing code snippet:
$('select').on('select2:open', function (e) {
$('.a').addClass("header-index");
});
$('select').on('select2:close', function (e) {
$('.a').removeClass("header-index");
});
How can I ensure that the close event is not triggered when a user attempts to open another select2 dropdown while one is already open?