
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to automatically close all collapsible elements inside of the accordion when closing the accordion?
We will use the bootstrap accordion component in our article to demonstrate how to collapse all the children's accordions inside the parent accordion. An accordion is a collapsible component that helps to display an expand/collapse type of content on the webpage.
In this article, we will use the Bootstrap 5 Accordion component to display a list of expandable/collapsible elements in a nested fashion. Now, first, we will listen to the “hide” collapse event by attaching an event listener to the parent accordion. After that, when the “hide” collapse event gets fired, we will find all the collapsible elements inside that accordion and consequently, remove the “show” class from the elements so as to hide them in the UI.
Example
Let’s discuss the approach with an example, below −
Event Listener to attach to the accordion −
“hide.bs.collapse”
Filename: index.html
<!DOCTYPE html> <html lang="en"> <head> <title>How to automatically close all collapsible elements inside of the accordion when closing the accordion?</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> </head> <body> <h3>How to automatically close all collapsible elements inside of the accordion when closing the accordion?</h3> <div class="accordion"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#parent"> Accordion Item #1 </button> <div id="parent" class="accordion-collapse collapse show"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#child-one"> Accordion sub Item #1 </button> <div id="child-one" class="accordion-collapse collapse show"> Accordion sub item 1 - body 1 </div> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#child-two"> Accordion sub Item #1 </button> <div id="child-two" class="accordion-collapse collapse show"> Accordion sub item 1 - body 1 </div> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#child-three"> Accordion sub Item #1 </button> <div id="child-three" class="accordion-collapse collapse show"> Accordion sub item 1 - body 1 </div> </div> </div> <script> const parent = document.getElementById('parent') parent.addEventListener('hide.bs.collapse', function() { const collapsibleEls = this.querySelectorAll('.collapse.show'); collapsibleEls.forEach(el => { el.classList.remove('show') }) }) </script> </body> </html>
Conclusion
In this article, we learned how to automatically close all the collapsible elements inside of the accordion when closing the parent accordion, and we achieved this by adding the “hide.bs.collapse” event listener on the parent collapsible element.
- Related Articles
- How to create Nested Accordion using Google AMP amp-accordion?
- Bootstrap Accordion
- How to Automatically Protect All Worksheets When Closing an Excel Workbook?
- How to create accordion in JavaFX?
- How to create Accordion in ReactJS?
- How to create an accordion hover effect with box-shadows in CSS
- How to close active/current tab without closing the browser in Selenium-Python?
- How to close the ResultSet cursor automatically, after commit in JDBC?
- How to Automatically Close Alerts using Twitter Bootstrap?
- How do we close resources automatically in Java?
- Selects all elements inside elements with CSS
- How to close all the opened files using Python?
- How to avoid dropdown menu to close menu items on clicking inside?
- How to Automatically Save and Close an Excel File after a Certain Idle Time?
- How to Automatically Open Files When Starting Excel?
