W3cubDocs

/Web APIs

ToggleEvent

The ToggleEvent interface represents an event notifying the user when a popover element's state toggles between showing and hidden.

It is the event object for the HTMLElement beforetoggle and toggle events, which fire on popovers when they transition between showing and hidden (before and after, respectively).

Event ToggleEvent

Note: ToggleEvent is unrelated to the HTMLDetailsElement toggle event, which fires on a <details> element when its open/closed state is toggled. Its event object is a generic Event.

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
ToggleEvent 114 114 114 No 100 17 114 114 No No 17 No
ToggleEvent 114 114 114 No 100 17 114 114 No No 17 No
newState 114 114 114 No 100 17 114 114 No No 17 No
oldState 114 114 114 No 100 17 114 114 No No 17 No

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ToggleEvent