I need help to make an anchor link open the corresponding accordion panel within an accordion. So far I've added the anchor link so that the page scrolls down to the panel, but I can't get it to open.
Link structure:
<a href="#CO">link text</a>
Accordion structure:
<button class="accordion" id="CO">button header</button>
<div class="panel">
<p>text</p>
</div>
JS structure:
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active");
/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
</script>
thank you!!
I would actually suggest trying to take a look into the details HTML tag.
https://www.w3schools.com/tags/tag_details.asp
It most likely has everything you need. All it needs is a bit of styling and you're good to go. Then you don't need to have additional JavaScript for toggling the visibility of the content.