I have a unique code that applies a style to the following div when another div is clicked, using nextElementSibling
.
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
console.log(panel)
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
<div class="accordion">
<p>Resources</p>
</div>
<div class=panel ">
Test message
</div>
This triggers a style change on the div with the class panel
. I want to move the class accordion
from the div
to the p
tag.
Here's how it should look:
<div>
<p class="accordion">Resources</p>
</div>
<div class= panel">
Test message
</div>
Now that accordion
is no longer next to panel
, how can I target the panel?