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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-02-02 11:00:00 +0000
Seen: 12 times
Last updated: Jun 21 '22
Is it feasible to utilize a Toggle Button to switch among multiple classes exceeding 2?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can I create a transition on click using CSS without the need for JavaScript?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
How can I resolve the issue of being unable to use Fetch to POST an array of Selected Checkboxes?