Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To set up a single button that can toggle the opening and closing of all accordion elements simultaneously, you can use JavaScript to manipulate the class names of the accordion element containers.

First, add a button element to your HTML code:

<button id="toggle-all">Toggle All</button>

Next, add an event listener to the button element that will toggle the class names of all the accordion element containers:

const toggleAllButton = document.getElementById('toggle-all');
const accordionContainers = document.querySelectorAll('.accordion-container');

toggleAllButton.addEventListener('click', function() {
  accordionContainers.forEach(function(container) {
    container.classList.toggle('active');
  });
});

In this example, we first select the toggle-all button element and all the accordion-container elements using their respective selectors. We then add an event listener to the toggle-all button that will toggle the 'active' class name of all the accordion-containers when the button is clicked.

Note that this method assumes that each accordion container has an 'active' class that is toggled to open or close it. If your accordion uses a different class name or method for opening and closing the containers, you will need to modify the code accordingly.