Avatar of Hobert Cruz
Hobert Cruz

asked on 

Convert addEventListener to real function..

Hi experts, how to convert the code below into real function? I need to convert the code from addEventListener to real function like, function doit(){}.  Thank you!

<script>
  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";
      }
    });
  }
</script>

Open in new window

JavaScript

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon