var coll = document.getElementsByClassName("collapsed"); var i; for (i = 0; i < coll.length; i++) { coll[i].nextElementSibling.style.display = "none"; coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); }