HTML Details open Property: Show or Hide Details Content

The HTML <details> element is used to create an interactive widget that the user can open and close. Using the open property, you can programmatically control whether the details are visible or hidden when the page loads or in response to user interactions. This guide will walk you through the syntax, usage, and practical examples of the open property.

What is the open Property?

The open property of the HTML <details> element determines whether the content within the <details> element is visible or hidden. By default, the content is hidden until the user clicks on the <summary> element to reveal it. However, using the open property, you can set the initial state of the <details> element to be either open or closed.

Syntax

The open property is a boolean attribute. When present, it indicates that the details should be visible (open). When absent, the details are hidden (closed).

HTML:

<details open>
  <summary>Details Title</summary>
  <p>Some content to be shown when the details are open.</p>
</details>

JavaScript:

  • Getting the open property:
const detailsElement = document.getElementById("myDetails");
const isOpen = detailsElement.open; // Returns true if open, false otherwise
  • Setting the open property:
const detailsElement = document.getElementById("myDetails");
detailsElement.open = true; // Opens the details
detailsElement.open = false; // Closes the details

Details Attributes

Here’s a table summarizing the key attributes of the <details> element:

Attribute Type Description
`open` Boolean Specifies whether the details should be visible upon page load. If present, the details are open; otherwise, they are closed.

Practical Examples

Let’s explore some practical examples to understand how to use the open property effectively.

Basic Usage: Setting Initial State

You can set the initial state of the <details> element directly in the HTML using the open attribute.

<details open id="detailsInitial">
  <summary>Initial Open Details</summary>
  <p>This content is visible by default because the 'open' attribute is present.</p>
</details>

In this example, the content inside the <details> element will be visible when the page loads.

Toggling Details with JavaScript

You can use JavaScript to toggle the open property in response to user interactions, such as button clicks.

<details id="detailsToggle">
  <summary>Toggle Details</summary>
  <p>This content will toggle when the button is clicked.</p>
</details>

<button id="toggleButton">Toggle Details</button>

<script>
  const detailsToggleElement = document.getElementById("detailsToggle");
  const toggleButtonElement = document.getElementById("toggleButton");

  toggleButtonElement.addEventListener("click", function () {
    detailsToggleElement.open = !detailsToggleElement.open;
  });
</script>

In this example, clicking the button will toggle the visibility of the details content.

Controlling Details Based on Conditions

You can programmatically control the open property based on certain conditions, such as user roles or application state.

<details id="detailsConditional">
  <summary>Conditional Details</summary>
  <p>This content is visible based on a condition.</p>
</details>

<script>
  const detailsConditionalElement = document.getElementById("detailsConditional");
  const userRole = "admin"; // Simulate a user role

  if (userRole === "admin") {
    detailsConditionalElement.open = true; // Open details for admin users
  } else {
    detailsConditionalElement.open = false; // Close details for non-admin users
  }
</script>

In this example, the details content is only visible if the userRole is “admin”.

Using the toggle Event

The <details> element also triggers a toggle event when its open state changes. You can listen for this event to perform additional actions.

<details id="detailsEvent">
  <summary>Details with Toggle Event</summary>
  <p>This content triggers a toggle event.</p>
</details>

<div id="eventOutput"></div>

<script>
  const detailsEventElement = document.getElementById("detailsEvent");
  const eventOutputElement = document.getElementById("eventOutput");

  detailsEventElement.addEventListener("toggle", function () {
    if (detailsEventElement.open) {
      eventOutputElement.textContent = "Details opened.";
    } else {
      eventOutputElement.textContent = "Details closed.";
    }
  });
</script>

In this example, the eventOutput div will display a message indicating whether the details were opened or closed.

Enhancing Accessibility

When using the open property, ensure that the <summary> provides a clear and concise description of the details content. This helps users understand what to expect when they open the details, improving the overall accessibility of your page.

<details id="detailsAccessible">
  <summary>Frequently Asked Questions (FAQ)</summary>
  <p>Here are some frequently asked questions and their answers.</p>
  <!-- FAQ content -->
</details>

In this example, the summary “Frequently Asked Questions (FAQ)” clearly indicates the type of content contained within the details.

Real-World Applications of the open Property

The open property is used in various scenarios, including:

  • FAQ Sections: Displaying answers to frequently asked questions in a collapsible format.
  • Configuration Panels: Showing advanced settings only when needed.
  • Documentation: Providing detailed explanations that can be expanded or collapsed.
  • Mobile Navigation: Creating expandable menus for better mobile user experience.

Use Case Example: Creating an Accordion Menu

Let’s create a practical example that demonstrates how to use the open property to build an accordion menu. This type of menu allows users to expand one section at a time, collapsing others.

<div class="accordion" id="accordionMenu">
  <details id="details1">
    <summary>Section 1</summary>
    <p>Content for Section 1.</p>
  </details>

  <details id="details2">
    <summary>Section 2</summary>
    <p>Content for Section 2.</p>
  </details>

  <details id="details3">
    <summary>Section 3</summary>
    <p>Content for Section 3.</p>
  </details>
</div>

<script>
  const accordionMenuElement = document.getElementById("accordionMenu");
  const detailsElements = accordionMenuElement.querySelectorAll("details");

  detailsElements.forEach((details) => {
    details.addEventListener("toggle", function () {
      if (details.open) {
        detailsElements.forEach((otherDetails) => {
          if (otherDetails !== details) {
            otherDetails.open = false;
          }
        });
      }
    });
  });
</script>

In this example, only one section of the accordion menu can be open at a time. When a new section is opened, the previously open section is automatically closed.

Browser Support

The <details> element and its open property are supported by all modern browsers, including Chrome, Firefox, Safari, and Edge. This ensures that your collapsible content will render consistently across various platforms.

Note: Always test your <details> elements across different browsers and devices to ensure a consistent user experience. 🧐

Conclusion

The HTML <details> open property is a powerful tool for controlling the visibility of content within a <details> element. Whether you are setting the initial state, toggling visibility with JavaScript, or creating complex accordion menus, understanding how to use the open property effectively can greatly enhance the user experience of your web pages. Happy coding!