I have a onClick function in external file navigation.js, which is included at the bottom of page. This is navigation.js:
( function() {
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
var open = document.getElementById("openNav");
var close = document.getElementById("closeNav");
open.addEventListener("click", openNav, false);
close.addEventListener("click", closeNav, false);
} )();
And this is part of wordpress page where click event should occure:
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" id="closeNav">×</a>
<?php
wp_nav_menu( array(
'theme_location' => 'menu-2',
'menu_id' => 'primary-menu-bottom',
'walker' => new My_Walker(),
) );
?>
</div>
<span class="open-menu" style="font-size:30px;cursor:pointer" id="openNav">☰ open</span>
That is actually hamburger menu, when screen is resized. Problem is that function is not working. Can I call onClick events from external js files, or I must put it at the same page where event should occure ?
openandclose? If they're undefined you may need to wait for theDOMContentLoadedevent or there's something else amiss.