Favorites

HTML onbeforetoggle Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onbeforetoggle
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 22nd November 2026

Description

The onbeforetoggle event attribute runs a script just before a popover element is shown or hidden. In recent browsers it also fires on <dialog> elements before they open or close. It does not fire on <details> elements, which only have the ontoggle event.

The handler receives a ToggleEvent with two properties:

  • event.oldState - the state before the change, "open" or "closed".
  • event.newState - the state after the change, "open" or "closed".

When the element is about to open (newState is "open"), the event is cancelable: calling event.preventDefault() keeps the popover or dialog closed. When it is about to close, the event cannot be canceled.

<button popovertarget="menu">Menu</button>
<div id="menu" popover onbeforetoggle="checkToggle(event)">Popover content</div>
function checkToggle(event) {
  if (event.newState === "open" && !userIsLoggedIn) {
    event.preventDefault(); // the popover stays closed
  }
}

The order of events is: beforetoggle fires first, the element opens or closes, and then toggle fires asynchronously.

Use Cases
  • Loading or updating the content of a popover just before it is shown.
  • Preventing a popover from opening under certain conditions.
  • Starting or stopping animations or timers that depend on the popover being visible.

Syntax

<element onbeforetoggle="script(event)">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
<html>
<head>
<title>onbeforetoggle event</title>
<style>
[popover] {
padding: 10px 20px;
border: 2px solid #4361ee;
}
</style>
</head>
<body>
<button popovertarget="pop">Toggle popover</button>
<label><input type="checkbox" id="allow" checked> Allow opening</label>
<div id="pop" popover onbeforetoggle="beforeToggle(event)" ontoggle="afterToggle(event)">
<p>This is a popover.</p>
</div>
<ul id="log"></ul>
<script>
function log(text) {
var li = document.createElement("li");
li.textContent = text;
document.getElementById("log").appendChild(li);
}
function beforeToggle(event) {
log("beforetoggle: " + event.oldState + " -> " + event.newState);
if (event.newState === "open" && !document.getElementById("allow").checked) {
event.preventDefault();
log("Opening prevented!");
}
}
function afterToggle(event) {
log("toggle: the popover is now " + event.newState);
}
</script>
</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onbeforetoggle event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee