This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
* Some parts of this feature may have varying levels of support.
The toggle event of the HTMLElement interface fires on a popover element, <dialog> element, or <details> element just after it is shown or hidden.
event.oldState property will be set to closed and the event.newState property will be set to open.event.oldState will be open and event.newState will be closed.This event is not cancelable.
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("toggle", (event) => { })
ontoggle = (event) => { }
A ToggleEvent. Inherits from Event.
The example code below demonstrates how the toggle event might be used for popover. The same code is might be used for a <dialog> or <details> elements in the same way.
This example shows how to listen for the toggle event and log the result.
The HTML consists of a popover and a button for toggling it open and closed.
<button popovertarget="mypopover">Toggle the popover</button> <div id="mypopover" popover>Popover content</div>
The code adds an event listener for the toggle event and logs the state.
const popover = document.getElementById("mypopover");
popover.addEventListener("toggle", (event) => {
if (event.newState === "open") {
console.log("Popover has been shown");
} else {
console.log("Popover has been hidden");
}
});
If multiple toggle events are fired before the event loop has a chance to cycle, only a single event will be fired. This is referred to as "event coalescing".
For example:
popover.addEventListener("toggle", () => {
// …
});
popover.showPopover();
popover.hidePopover();
// `toggle` only fires once
HTMLDialogElement
| Specification |
|---|
| HTML> # event-toggle> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
toggle_event |
36 | 79 | 49 | 23 | 10.1 | 36 | 49 | 24 | 10.3 | 3.0 | 37 | 10.3 |
details_elements |
36 | 79 | 49 | 23 | 10.1 | 36 | 49 | 24 | 10.3 | 3.0 | 37 | 10.3 |
dialog_elements |
132 | 132 | 133 | 117 | 26 | 132 | 133 | 87 | 26 | No | 132 | 26 |
popover_elements |
114 | 114 | 125 | 100 | 17 | 114 | 125 | 76 | 17 | 23.0 | 114 | 17 |
popover HTML global attributebeforetoggle
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/toggle_event