HTML ontoggle Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

toggle

open changes.

03

details

FAQ panels.

04

dialog

Modals.

05

.open

Read state.

06

addEventListener

Preferred way.

Purpose of ontoggle Attribute

The 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.

💡
Not for checkboxes

ontoggle does not work on <input type="checkbox">. Use onchange when a checkbox checked state changes.

📝 Syntax

Set ontoggle on <details> or <dialog>, or assign element.ontoggle in JavaScript:

ontoggle.html
<details ontoggle="handleToggle()">
  <summary>FAQ</summary>
  <p>Answer content.</p>
</details>

<script>
  el.ontoggle = handleToggle;
  el.addEventListener("toggle", handleToggle);
</script>

Syntax Rules

  • Value is JavaScript executed when the toggle event fires.
  • Valid on <details> and <dialog> elements.
  • Fires when the open attribute is set or removed.
  • Read event.target.open or element.open for current state.
  • Also assignable as element.ontoggle in script.
  • Modern alternative: addEventListener("toggle", handler).
  • Not for <input type="checkbox"> — use onchange.

💎 Values

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.
  • JavaScript: details.ontoggle = () => { … } — property assignment.
ontoggle-handler.js
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);
  }
});

⚡ Quick Reference

Property / APIWhen it runsNotes
toggleopen attribute changesontoggle
element.openRead in handlertrue = open
<summary> clickToggles detailsNative behavior
dialog.showModal()Opens dialogFires toggle
dialog.close()Closes dialogFires toggle
change on checkboxChecked state changesUse onchange, not ontoggle

Applicable Elements

ElementSupported?Notes
<details>YesPrimary use case — disclosure widgets
<dialog>YesModal / non-modal open state
<summary>NoPut handler on parent details
<input type="checkbox">NoUse onchange instead
<button>NoUse onclick for button clicks

ontoggle vs onchange vs onclick

HandlerWhen it firesTypical use
ontoggleopen on details/dialogFAQ expand, modal state
onchangeForm control value changesCheckbox, select, input
onclickElement clickedButtons, links
open attributeSet in HTML or JSStart details expanded

Examples Gallery

Details disclosure handler, dynamic assignment, and dialog toggle with addEventListener.

👀 Live Preview

Click the summary — status updates when details opens or closes:

Learn more

Hidden content revealed when open.

Collapsed

Example — ontoggle on details

React when an FAQ panel expands or collapses:

details-ontoggle.html
<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>
Try It Yourself

How It Works

Clicking <summary> toggles the open attribute on details, which fires toggle and runs your handler.

Dynamic Values with JavaScript

Assign the handler on a details element at runtime:

dynamic-ontoggle.html
<script>
  document.getElementById("dynamicToggle").ontoggle = function () {
    log.textContent = this.open ? "Now open" : "Now closed";
  };
</script>
Try It Yourself

How It Works

Assigning element.ontoggle is equivalent to the inline attribute. Use this.open inside the function to read state.

Example — toggle on dialog

Track when a native modal dialog opens or closes:

dialog-toggle.html
dialog.addEventListener("toggle", function (event) {
  status.textContent = event.target.open
    ? "Dialog open"
    : "Dialog closed";
});

openBtn.addEventListener("click", () => dialog.showModal());
closeBtn.addEventListener("click", () => dialog.close());
Try It Yourself

How It Works

<dialog> also supports the toggle event when its open state changes via showModal(), show(), or close().

♿ Accessibility

  • Use native details/summary — Browsers provide keyboard support for disclosure widgets.
  • Announce state changes — Use aria-live="polite" on status regions updated by toggle.
  • Do not break summary — Keep <summary> as the first child of details.
  • Dialog focus — Modals trap focus; ensure close controls are keyboard-accessible.
  • Avoid alert() — Use visible on-page feedback for expand/collapse state.

🧠 How ontoggle Works

1

User toggles

Summary click.

Action
2

open changes

Attribute set/removed.

State
3

toggle fires

ontoggle runs.

Event
=

React to state

Read .open.

Browser Support

The toggle event on <details> is supported in all modern browsers. <dialog> toggle follows native dialog support.

details · Fully supported

Universal ontoggle on details

All major browsers fire toggle when details open state changes.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Not supported
No details toggle
Opera Fully supported
Full support
ontoggle on details 99% supported

Bottom line: Reliable for FAQ and disclosure widgets in all modern browsers.

💡 Best Practices

✅ Do

  • Use ontoggle on details and dialog
  • Read element.open to know expanded state
  • Use addEventListener("toggle", …) in production
  • Lazy-load heavy content when details opens
  • Pair with accessible summary labels

❌ Don’t

  • Put ontoggle on checkboxes — use onchange
  • Use alert() for every expand/collapse
  • Assume toggle fires on all clickable elements
  • Replace native details without keyboard support
  • Confuse toggle with click on summary

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ontoggle

Bookmark these before wiring disclosure handlers.

5
Core concepts
📄 02

details

FAQ panels.

Where
💬 03

dialog

Modals too.

Also
04

.open

true / false.

Read
05

Not checkbox

use onchange.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the toggle event fires — when the open attribute is added or removed on a details or dialog element.
No. Checkboxes fire the change event when checked state changes. Use onchange instead.
Read detailsElement.open inside your handler. It is true when expanded and false when collapsed.
Both can occur, but attach 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.
Yes for details in all modern browsers. Dialog toggle support matches native <dialog> support.

Build interactive disclosure widgets

Practice the ontoggle attribute with details and dialog demos in the Try It editor.

Try details toggle demo →

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.

5 people found this page helpful