What event fires after you click on , but I doubt it will fire anything you could be waiting for). – Kaiido Jul 03 '20 at 07:47

2 Answers2

1

There's no such event that signifies that the <select> is opened, but no option is selected and the user clicks away.

However, for <select> element, you can bind to click event which does not warrant the selection of an option to fire and write custom logic. I just noticed that it requires 2 clicks on <select> element for an option to be selected and for opening it only needs 1 click. With the click count, we can determine if the preconditions meet and accordingly run the cleanup code:

var clicks = 0,
    $select = document.getElementById('select');
$select.onclick = function() {
  clicks++;

  if (clicks === 2) {
    clicks = 0;
  }
};
$select.onblur = function(e) {
  if (clicks === 1) {
    clicks = 0;

    // SELECT WAS OPENED BUT NO OPTION SELECTED
    // CLEANUP CODE GOES HERE

    console.log('CLEAN UP');
  }
};
<select id="select">
  <option>A</option>
  <option>B</option>
</select>

Like @leisheng and @Kaiido suggest, this is not universal solution and will only work with desktops and mouse.

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • 2
    Downvote for using jQuery in a question that does not mention jQuery. – Adrian Brand Jul 03 '20 at 01:34
  • The select menu can also be opened with the keyboard by using tab to focus and spacebar to open. – leisheng Jul 03 '20 at 02:26
  • @AdrianBrand Thanks for the feedback. Check the edits. It's pure JS now. – Muhammad Talha Akbar Jul 03 '20 at 07:14
  • @AdrianBrand Kudos, for giving a reason for the downvote, wish more down-voters did the same, as I believe that is when it becomes helpful. – Keith Jul 03 '20 at 08:52
  • @leisheng It's true that keyboard is also used to open the `` comes into focus, subsequent keypresses such as `space` are not fired in the keyboard handlers. If you have something to improve this answer, please feel free to edit. – Muhammad Talha Akbar Jul 03 '20 at 09:34
1

As far as what events occur when you open a select menu there are many. There may very well be a way to create your own CustomEvent and fire it by tracking the other events. If you can see what events are happening, you might be able to figure something out. This is a script I use to find all events that are occurring on an EventTarget:

EventTarget.prototype.__original_addEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(listener, callback) {
    if (listener === 'event') {
        for (let key in this) {
            if (key.startsWith('on')) {
                let eventName = key.substr(2, key.length);
                this.__original_addEventListener(eventName,
                function(e) {
                    callback(e);
                });
            }
        }
    } else {
        this.__original_addEventListener(listener, callback);
    }
};

window.onload = function() {
    document.querySelector('#id-of-my-select-tag').addEventListener('event', (e) => {
        console.log(e.type);
    });
};

This just allows you to add a listener called 'event' which captures all events. This will output all the events happening and maybe you will see a pattern that helps you write some code to detect when it is open based on the events that occurred and the order in which they occurred. (Tested in Firefox, Chrome and Edge)

leisheng
  • 340
  • 1
  • 8
  • Thanks for the nice idea how to investigate thism but there appears to be no event that triggers right after. – Morgan Wilde Jul 03 '20 at 05:25
  • Note that not all event listeners do map to an `on...` IDL, for instance `DOMContentLoaded` doesn't. – Kaiido Jul 03 '20 at 07:45
  • 1
    @MorganWilde you could possibly chain event listeners to see what events occurred in what order. For example, when a mousedown event occurs, then add an event listener for mouseleave, which appears to be triggered automatically after mousedown on a select tag. The select tag events are a bit confusing because there are many different possibilities. For example, the user can mousedown and then only mouse up once they have selected a different option, so the click event is not reliable for detection. Or they can click, then use the keyboard. You'd have to account for many situations. – leisheng Jul 03 '20 at 20:59