I’m new to javascript and have just worked out how to make an accordion on an HTML page on our company website. The accordion is displaying correctly on the page, however when you click to open it, it stays open for a few seconds and then automatically takes you to the top of the page and away from the accordion. I don’t know what I’ve done, this is the code that I’ve put onto the page for the accordion:
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
</script>
Please help!
Thank you