I've been working on creating an accordion panel using CSS with a touch of JavaScript.
Everything seems to be functioning correctly except for the toggling of the panels.
Essentially, when one panel is clicked, I want all other open panels to close.
Check out this fiddle:
I attempted to achieve this by keeping all panels closed all the time with the following code:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
$('.panel').addClass('hide');
this.nextElementSibling.classList.toggle("hide");
this.nextElementSibling.classList.toggle("show");
}
}
and in the CSS section:
.panel.hide {
opacity: 0;
height: 0;
}
I also tried:
$(this).removeClass('hide');
I understand that jQuery was used and not pure JavaScript but it didn't seem to work as expected. Any advice on how to fix this issue would be greatly appreciated.
Thank you in advance.