HTML ontoggle Event Attribute

The HTML ontoggle event attribute is triggered when a user opens or closes the <details> HTML element in an HTML document.


Following is the syntax −

<details ontoggle=”script”>Content</details>

Let us see an example of HTML ontoggle event Attribute −


 Live Demo

<!DOCTYPE html>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
<h1>HTML ontoggle Event Attribute Demo</h1>
<details ontoggle="toggleFn()">
<summary>Hi! I'm summary element</summary>
<p>This is a paragraph with some dummy content.</p>
   function toggleFn() { = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat"; = "#fff";


Now try to open the details HTML element to observe how ontoggle event attribute works.