HTML ontoggle Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The ontoggle attribute is an event-handler attribute. It runs JavaScript when the toggle event fires — just after a <details>, <dialog>, or popover element is shown or hidden.

For beginners, the usual target is <details> with a <summary>. Related: the open attribute/property, and onchange (form values — not disclosure widgets).

Baseline · Widely available.

Not deprecated. Per MDN, toggle is not cancelable. Details support is solid; some related parts (for example popover) have newer or varying support.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer el.addEventListener("toggle", handler) in production; use inline ontoggle for quick demos and learning.

HTML Syntax

syntax.html
<details|dialog ontoggle=" script">

script is JavaScript that runs after open/close. Equivalent JS: el.ontoggle = handler or el.addEventListener("toggle", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run after open/closeTry it
ontoggle="fn()"Named handler; read details.openTry it
newState / oldStateToggleEvent fields: open or closedTry it
Start with openDetails can load already expandedTry it

Live Preview

Open and close the disclosure. Status updates on each toggle.

Details

Click the summary (or use keyboard) to expand and collapse.

Preview
More information

Hidden content appears when open.

Waiting for toggle…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Status from details.open

Update a line when the disclosure expands or collapses.

HTML
<details id="info" ontoggle="onToggle()">
  <summary>Shipping info</summary>
  <p>Free shipping over $50.</p>
</details>
<p id="status">Closed</p>

<script>
  function onToggle() {
    var el = document.getElementById("info");
    document.getElementById("status").textContent =
      el.open ? "Open" : "Closed";
  }
</script>
Try It Yourself

How It Works

  1. Put ontoggle on <details> — not on <summary>.
  2. User opens or closes the widget (click or keyboard).
  3. toggle fires after the state change.
  4. Read details.open — true when expanded.

Example 2 — ToggleEvent newState

Use event.newState (open or closed).

HTML
<details id="faq">
  <summary>FAQ</summary>
  <p>Answers go here.</p>
</details>
<p id="log">Waiting…</p>

<script>
  var faq = document.getElementById("faq");
  var log = document.getElementById("log");

  faq.addEventListener("toggle", function (event) {
    log.textContent =
      event.oldState + " → " + event.newState;
  });
</script>
Try It Yourself

How It Works

  1. Listen with addEventListener for a ToggleEvent.
  2. oldState was open or closed before the change.
  3. newState is the state after the change.
  4. Same idea for dialog/popover — MDN uses the same event shape.

Example 3 — Start Open with the open Attribute

Load expanded; still get toggle when the user closes it.

HTML
<details id="panel" open ontoggle="sync()">
  <summary>Already open</summary>
  <p>Close me to fire toggle.</p>
</details>
<p id="note">open is true on load.</p>

<script>
  function sync() {
    var panel = document.getElementById("panel");
    document.getElementById("note").textContent =
      "Now open = " + panel.open;
  }
</script>
Try It Yourself

How It Works

  1. open makes the details start expanded.
  2. Boolean attribute: presence means open — do not use open="false" to close.
  3. toggle does not fire on first paint for that initial open — only on later changes.
  4. User closes/opens afterward → ontoggle runs.

Edge Cases & Pitfalls

Most mistakes are putting ontoggle on a checkbox or expecting it before the state changes.

target

Not for checkboxes

Use onchange (or input) for input type="checkbox". ontoggle is for details / dialog / popover open state.

timing

Fires after the change

Per MDN, the event runs just after show/hide. It is not cancelable — you cannot stop the open/close with preventDefault().

coalesce

Event coalescing

If open/close happens multiple times before the event loop runs (for example scripted show then hide), browsers may fire only one coalesced toggle.

open

open="false" still opens

Boolean attributes: remove open entirely to close. Writing open="false" still counts as present.

name

Named details groups

Several <details name="…"> with the same name act like an accordion — opening one closes another, and each change can fire toggle.

Browser Support

ontoggle is an HTML event-handler attribute for details/dialog/popover open state in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

ontoggle

Details toggle is widely available in modern Chrome, Firefox, Safari, and Edge. Dialog and popover toggle follow those features’ support.

95% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer —
No details/toggle
ontoggle Widely supported (details)

Bottom line: Safe for details disclosures in modern browsers. Prefer addEventListener('toggle'), read open or newState, and do not use it for checkboxes.

Key Takeaways

  • Role: details/dialog/popover just opened or closed.
  • Not cancelable — reacts after the state change.
  • Read state: element.open or event.newState.
  • Not onchange — checkboxes use form change/input events.

One line: ontoggle means a disclosure (or dialog/popover) finished opening or closing — read open or newState.

Frequently Asked Questions

It is an event-handler attribute. When a details element, dialog, or popover finishes opening or closing, the toggle event fires and the JavaScript in ontoggle runs.
Per MDN: details, dialog, and popover elements. Beginners usually start with details/summary. It is not for checkboxes — use onchange there.
Read details.open (true/false), or use ToggleEvent: event.newState is open or closed (and event.oldState is the previous state).
No. Per MDN, the toggle event is not cancelable. It fires after the open/closed change has already happened.
ontoggle is for open/closed UI such as details. onchange is for form control values (checkbox, select, text after commit).
No. The toggle event and ontoggle handler are standard. Support is Baseline widely available for details; some related surfaces (for example popover) have newer or varying support.

Next: onunload

Run JavaScript when the document is being unloaded.

onunload tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful