I have some HTML that is created dynamically. So the click
event not working on that. To solve this I added document
before addEventListener
in my below code like this
document.acc[i].addEventListener
But it's not working. Can anyone tell how we can add document
in this code so that event will work perfectly on the dynamically created elements.
$(document).ready(function () {
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;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
});