Created
April 27, 2022 01:21
-
-
Save jbd91/0233834e9b97691d25a2cf87faf888e2 to your computer and use it in GitHub Desktop.
Accordion Toggle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var acc = document.getElementsByClassName("accordion__toggle"); | |
| var i; | |
| for (i = 0; i < acc.length; i++) { | |
| acc[i].addEventListener("click", function () { | |
| var panel = this.nextElementSibling; | |
| if (this.classList.contains('active')) { | |
| this.classList.remove("active"); | |
| panel.classList.remove("active"); | |
| panel.setAttribute("aria-expanded", "false"); | |
| } else { | |
| document.querySelectorAll(".accordion__toggle, .accordion__panel").forEach(function(item){ | |
| item.classList.remove('active'); // Remove the class | |
| }); | |
| this.classList.add("active"); | |
| panel.classList.add("active"); | |
| panel.setAttribute("aria-expanded", "true"); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment