When shown, hidden and destroyed, the instance will emit certain events. It is possible to subscribe to these with the
on() method which will receive the dialog container element and the event object (if any).
The event object can be used to know which trigger (opener / closer) has been used in case of a
You can unregister these handlers with the
For auto-instantiated dialogs though, registering event listeners on the instance is not possible since there is no access to the dialog instance itself. To work around the problem while still relying on auto-instantiation, it is possible to listen for DOM events on the dialog container.
For events triggered from interacting with a UI element (such as opening or closing with a button), the original click event is passed in the
detail key of the DOM event. For instance, to access the button that was interacted with to open the dialog, one can use
Because these event listeners can be registered on the DOM element, before the dialog gets instantiated at all (or even before the script gets even loaded), it is possible to react to dialog creation (with the
create event), which is not possible otherwise.
You can unregister these event listeners with the
removeEventListener() method, just like any other DOM event.