How to add simple js to theme </body>

I am trying to add simple javascript to in CSS/HTML edit in Themes.

Effectively the ability to click buttons to expand content with addEventListener

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  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";
    }
  });
}
</script>

However, this script doesn’t seem to appear in the source code.

Any help?