I'm trying to modify the w3schools accordion so that it closes all panels before opening the clicked panel:
var acc = document.getElementsByClassName("accordion-trigger");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
acc.classList.remove("active"); //This is the line I've added
this.classList.add("active"); //Changed to add from toggle
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
My modification doesn't work - I get
acc.classList is undefined
I'm familiar with jQuery syntax, but I don't understand what else I need to do here in JS. Where have I gone wrong?