I have a series of anchor links at the top of my page and a series of accordions at the bottom of the page. My goal is when someone clicks the anchor link, it will drop them down to the corresponding accordion and open it automatically. The accordion elements work when you click on them directly, but I want them to also be triggered by the anchor links. *NOTE: I am using WordPress so these elements are generated using a loop. Here's a very simplified version of what I'm working with (obviously without the loop):
var directoryAcc = document.getElementsByClassName("directory-accordion");
for (var i = 0; i < directoryAcc.length; i++) {
directoryAcc[i].addEventListener("click", function() {
var panel = this.nextElementSibling;
if (panel.style.display === "flex") {
panel.style.display = "none";
} else {
panel.style.display = "flex";
}
});
}
.container {
display: flex;
flex-direction: column;
}
.directory-accordion {
cursor: pointer;
}
.panel {
display: none;
}
<div class="container">
<a href="#" class="link">Link1</a>
<a href="#" class="link">Link2</a>
<a href="#" class="link">Link3</a>
</div>
<hr>
<div class="container">
<span class="directory-accordion">Text1</span>
<p class="panel">Accordion text1</p>
<span class="directory-accordion">Text2</span>
<p class="panel">Accordion text2</p>
<span class="directory-accordion">Text3</span>
<p class="panel">Accordion text3</p>
</div>
Any help would be appreciated, thank you!