👀 Live Preview
Click the summary — status updates when details opens or closes:
Learn more
Hidden content revealed when open.
Collapsed

The ontoggle attribute is an inline event handler that runs JavaScript when the toggle event fires. This happens when the open attribute is added or removed on a <details> or <dialog> element — for example when a user clicks a <summary> to expand an FAQ panel, or when a modal dialog opens and closes. Use it to track disclosure state, run analytics, or sync custom UI. It does not fire on checkboxes — use onchange for those. Prefer addEventListener("toggle", …) in production.
Inline JS.
open changes.
FAQ panels.
Modals.
Read state.
Preferred way.
ontoggle AttributeThe primary purpose of ontoggle is to react when a disclosure widget or dialog changes between open and closed — so your page can update status text, lazy-load content inside a <details>, log analytics, or coordinate multiple accordion sections.
The toggle event fires after the open attribute changes. Check element.open inside your handler to know the new state. For checkboxes and switches, the browser fires change, not toggle.
ontoggle does not work on <input type="checkbox">. Use onchange when a checkbox checked state changes.
Set ontoggle on <details> or <dialog>, or assign element.ontoggle in JavaScript:
<details ontoggle="handleToggle()">
<summary>FAQ</summary>
<p>Answer content.</p>
</details>
<script>
el.ontoggle = handleToggle;
el.addEventListener("toggle", handleToggle);
</script>toggle event fires.<details> and <dialog> elements.open attribute is set or removed.event.target.open or element.open for current state.element.ontoggle in script.addEventListener("toggle", handler).<input type="checkbox"> — use onchange.The ontoggle attribute accepts a string of JavaScript code:
ontoggle="handleToggle()" — Call a named function.ontoggle="logState(event)" — Pass the event object in inline handlers.details.ontoggle = () => { … } — property assignment.function handleToggle(event) {
var el = event.target;
status.textContent = el.open ? "Expanded" : "Collapsed";
}
details.addEventListener("toggle", function (event) {
if (event.target.open) {
loadFaqContent(event.target);
}
});| Property / API | When it runs | Notes |
|---|---|---|
toggle | open attribute changes | ontoggle |
element.open | Read in handler | true = open |
<summary> click | Toggles details | Native behavior |
dialog.showModal() | Opens dialog | Fires toggle |
dialog.close() | Closes dialog | Fires toggle |
change on checkbox | Checked state changes | Use onchange, not ontoggle |
| Element | Supported? | Notes |
|---|---|---|
<details> | Yes | Primary use case — disclosure widgets |
<dialog> | Yes | Modal / non-modal open state |
<summary> | No | Put handler on parent details |
<input type="checkbox"> | No | Use onchange instead |
<button> | No | Use onclick for button clicks |
ontoggle vs onchange vs onclick| Handler | When it fires | Typical use |
|---|---|---|
ontoggle | open on details/dialog | FAQ expand, modal state |
onchange | Form control value changes | Checkbox, select, input |
onclick | Element clicked | Buttons, links |
open attribute | Set in HTML or JS | Start details expanded |
Details disclosure handler, dynamic assignment, and dialog toggle with addEventListener.
Click the summary — status updates when details opens or closes:
Hidden content revealed when open.
Collapsed
React when an FAQ panel expands or collapses:
<details ontoggle="toggleAction()">
<summary>FAQ section</summary>
<p>Answer content.</p>
</details>
<script>
function toggleAction() {
var d = document.querySelector("details");
status.textContent = d.open ? "Expanded" : "Collapsed";
}
</script>Clicking <summary> toggles the open attribute on details, which fires toggle and runs your handler.
Assign the handler on a details element at runtime:
<script>
document.getElementById("dynamicToggle").ontoggle = function () {
log.textContent = this.open ? "Now open" : "Now closed";
};
</script>Assigning element.ontoggle is equivalent to the inline attribute. Use this.open inside the function to read state.
Track when a native modal dialog opens or closes:
dialog.addEventListener("toggle", function (event) {
status.textContent = event.target.open
? "Dialog open"
: "Dialog closed";
});
openBtn.addEventListener("click", () => dialog.showModal());
closeBtn.addEventListener("click", () => dialog.close());<dialog> also supports the toggle event when its open state changes via showModal(), show(), or close().
aria-live="polite" on status regions updated by toggle.<summary> as the first child of details.Summary click.
Attribute set/removed.
ontoggle runs.
Read .open.
The toggle event on <details> is supported in all modern browsers. <dialog> toggle follows native dialog support.
ontoggle on detailsAll major browsers fire toggle when details open state changes.
Bottom line: Reliable for FAQ and disclosure widgets in all modern browsers.
ontoggle on details and dialogelement.open to know expanded stateaddEventListener("toggle", …) in productiondetails openssummary labelsontoggle on checkboxes — use onchangealert() for every expand/collapsedetails without keyboard supporttoggle with click on summaryThe ontoggle attribute runs JavaScript when details or dialog elements change their open state — useful for FAQ panels, accordions, and modal tracking.
Read element.open, use onchange for checkboxes instead, and prefer addEventListener in production code.
ontoggleBookmark these before wiring disclosure handlers.
State change.
WhatFAQ panels.
WhereModals too.
Alsotrue / false.
Readuse onchange.
Gotchatoggle event fires — when the open attribute is added or removed on a details or dialog element.change event when checked state changes. Use onchange instead.detailsElement.open inside your handler. It is true when expanded and false when collapsed.ontoggle to details to react to the open state change — not just the click.element.addEventListener("toggle", handler) is preferred in production — easier to attach multiple listeners and remove them later.details in all modern browsers. Dialog toggle support matches native <dialog> support.Practice the ontoggle attribute with details and dialog demos in the Try It editor.
5 people found this page helpful