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.

Syntax

Following is the syntax −

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

Let us see an example of HTML ontoggle event Attribute −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
   }
</style>
</head>
<body>
<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>
</details>
<script>
   function toggleFn() {
      document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
      document.body.style.color = "#fff";
   }
</script>
</body>
</html>

Output

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


Advertisements