HTML onbeforetoggle Event Attribute
Description
The onbeforetoggle event attribute is used to execute JavaScript code just before a <details> element is toggled - that is, before it is opened or closed. This event occurs when a user clicks the <summary> element inside a <details> element, or when the toggle state is changed programmatically.
It gives developers a chance to intercept the toggle action, validate conditions, or even prevent the toggle from happening by calling event.preventDefault().
Where It Can Be Used
- Can only be applied to the
<details>HTML element. - Example of
<details>usage:
<details onbeforetoggle="checkPermission(event)">
<summary>More info</summary>
<p>This content is hidden until toggled.</p>
</details>
Event Object
The onbeforetoggle event handler receives an event object with several properties:
event.target→ references the<details>element being toggled.event.defaultPrevented→ indicates ifpreventDefault()was called.event.preventDefault()→ method to stop the toggle from occurring.
The event is cancelable, meaning you can prevent the default behavior (opening or closing the <details> element).
Behavior
- Fires before the
<details>element toggles. - If
event.preventDefault()is called inside the handler, the toggle is canceled. - After the toggle, the
ontoggleevent fires (soonbeforetogglecomes first).
Example: Basic Usage
<details id="details1" onbeforetoggle="beforeToggleHandler(event)">
<summary>Show More</summary>
<p>Hidden content goes here.</p>
</details>
<script>
function beforeToggleHandler(event) {
// Check if user is allowed to toggle
const allowed = confirm("Do you want to toggle this details element?");
if (!allowed) {
event.preventDefault(); // Stops the toggle
alert("Toggle prevented!");
} else {
console.log("Toggle allowed");
}
}
</script>
What happens here:
- When the user clicks the
<summary>, theonbeforetogglehandler runs. - A confirmation dialog appears.
- If the user cancels,
event.preventDefault()stops the<details>from opening/closing.
Key Notes
Browser support: Most modern browsers support
onbeforetoggle, but older browsers may not.Cancelable event:
onbeforetoggleis the only<details>related event that can prevent the toggle.Sequence of events:
onbeforetoggle→ fires before toggle, cancelable<details>toggles (if not canceled)ontoggle→ fires after toggle, not cancelable
Use Cases
- Asking user confirmation before expanding/collapsing sensitive information.
- Preventing toggle under certain conditions (e.g., insufficient permissions, unsaved changes).
- Logging or analytics: track when users attempt to interact with collapsible content.
Syntax
<element onbeforetoggle="script(event)">
Values
- scriptThe name of the script to use when the event has been triggered.
Example
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
Tablets & Mobile
Last updated by CSSPortal on: 27th December 2025
