Search code examples
javascripthtmlcsshtml-helper

how to external JavaScript in HTML and CSS


here is a code of HTML including JS and I want the JS to be an external file but it wont work , how to solve it and make the element move while clicking when it external

<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>

Solution

  • if you want to use external JS file inside your html file then you have to put the script tag inside the head of the html file

    <head>
      <script src="file.js" defer></script>
    </head>