title: How to call a JavaScript function from Rails 6 view
body: <p>In my view I'm trying to call a JavaScript function which I've placed in application.js:</p>
  
  <pre><code>/* Set the width of the side navigation to 250px */
  function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
  }
  
  /* Set the width of the side navigation to 0 */
  function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
  }
  </code></pre>
  
  <p>This is my HTML:</p>
  
  <pre><code>&lt;div id="mySidenav" class="sidenav"&gt;
    &lt;a href="javascript:void(0)" class="closebtn" onclick="closeNav()"&gt;&amp;times;&lt;/a&gt;
    &lt;a href="#"&gt;About&lt;/a&gt;
    &lt;a href="#"&gt;Services&lt;/a&gt;
    &lt;a href="#"&gt;Clients&lt;/a&gt;
    &lt;a href="#"&gt;Contact&lt;/a&gt;
  &lt;/div&gt;
  
  &lt;span style="font-size:30px;cursor:pointer" onclick="openNav()"&gt;&amp;#9776; open&lt;/span&gt;
  </code></pre>
  
  <p>If I put this in my view within <code>&lt;script&gt;&lt;/script&gt;</code> tags, then it works. My <code>application.js</code> works and it's being compiled but for some reason it only works if the JavaScript is in the actual view of the <code>html.erb</code> file. But if it's in <code>application.js</code> in my pack folder, although it builds the JavaScript file, it returns this error in the console:</p>
  
  <pre><code>(index):23 Uncaught ReferenceError: openNav is not defined
      at HTMLSpanElement.onclick ((index):23)
  </code></pre>
  
  <p>How can I get this to work in my <code>pack/application.js</code> file rather than having it in the theme in a <code>&lt;script&gt;</code> tag?</p>