Javascript Ruby On Rails


I got stuck in a few lines of code with Javascript in Ruby On Rails.

<script>
  function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
  }

  // Close the dropdown if the user clicks outside of it
  window.onclick = function(e) {
    if (!e.target.matches('.dropbtn')) {
    var myDropdown = document.getElementById("myDropdown");
      if (myDropdown.classList.contains('show')) {
        myDropdown.classList.remove('show');
      }
    }
  }
</script>

If I leave this code on html file then it works fine. But when I remove <script></script> and leave this code on js file then it didn’t work anymore while other scripts still working. What’s problem do I have?

Source: JavaSript – Stack Overflow

November 10, 2021
Category : News
Tags: javascript | ruby-on-rails

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.