In my view I'm trying to call a JavaScript function which I've placed in application.js:
/* 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";
}
This is my HTML:
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
If I put this in my view within <script></script>
tags, then it works. My application.js
works and it's being compiled but for some reason it only works if the JavaScript is in the actual view of the html.erb
file. But if it's in application.js
in my pack folder, although it builds the JavaScript file, it returns this error in the console:
(index):23 Uncaught ReferenceError: openNav is not defined
at HTMLSpanElement.onclick ((index):23)
How can I get this to work in my pack/application.js
file rather than having it in the theme in a <script>
tag?