//Declare variable, add class name to get elements by class name var semesters = document.getElementsByClassName("semester"); for (let i = 0; i < semesters.length; i++) { var semester = semesters[i]; // Toggle between adding and removing the "active" class, to highlight the button that controls the panel semester.addEventListener("click", function () { this.classList.toggle("active"); var courses = this.nextElementSibling; //Slide down the content by setting a calculated max-height, depending on the panel's height on your screen size. if (courses.style.maxHeight) { courses.style.maxHeight = null; } else { courses.style.maxHeight = courses.scrollHeight + "px"; } }); }