ToggleEvent

Baseline 2025
Newly available

Since January 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The ToggleEvent interface represents an event notifying the user an Element's state has changed.

This is the event object for the HTMLElement beforetoggle and toggle events, which fire on some elements just before and just after they transition between showing and hidden, respectively.

Event ToggleEvent

Constructor

ToggleEvent()

Creates an ToggleEvent object.

Instance properties

This interface inherits properties from its parent, Event.

ToggleEvent.newState Read only

A string (either "open" or "closed"), representing the state the element is transitioning to.

ToggleEvent.oldState Read only

A string (either "open" or "closed"), representing the state the element is transitioning from.

Examples

Basic example

js
const popover = document.getElementById("mypopover");

// ...

popover.addEventListener("beforetoggle", (event) => {
  if (event.newState === "open") {
    console.log("Popover is being shown");
  } else {
    console.log("Popover is being hidden");
  }
});

Specifications

Specification
HTML
# toggleevent

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
ToggleEvent
ToggleEvent() constructor
newState
oldState

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also