HTML ontoggle Event Attribute
Description
The ontoggle HTML event attribute is fired when the user opens or closes a <details> element. The <details> element is used to provide additional information or details that the user can view or hide on demand.
To use the ontoggle event attribute, simply add it to the <details> element that you want to monitor. The value of the ontoggle attribute should be a JavaScript function that will be executed when the <details> element is opened or closed.
Here is an example of how to use the ontoggle event attribute:
<details ontoggle="myFunction()">
<summary>Open the details</summary>
<p>This is some additional information.</p>
</details>
function myFunction() {
console.log("The ontoggle event occurred!");
}
When the user clicks on the <summary> element to open the <details> element, the myFunction() function will be executed. Similarly, when the user clicks on the <details> element to close it, the myFunction() function will also be executed.
The ontoggle event attribute can be used to implement a variety of different features, such as:
- Accordion menus
- Tabs
- Spoilers
- Interactive tutorials
- And much more!
Syntax
<details ontoggle="script">
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 ontoggle 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: 14th October 2023
