Skip to content

Instantly share code, notes, and snippets.

@jbd91
Created April 27, 2022 01:21
Show Gist options
  • Select an option

  • Save jbd91/0233834e9b97691d25a2cf87faf888e2 to your computer and use it in GitHub Desktop.

Select an option

Save jbd91/0233834e9b97691d25a2cf87faf888e2 to your computer and use it in GitHub Desktop.
Accordion Toggle
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