I want to call a function on button click event . The function is defined in another js file and I have enqueue
that file in my function.php
file. I can see that the file has been loaded from enqueue
. But I got error
errorReferenceError: myFunction is not defined
when I click on that button. I want to display dropdown menu on that button click
<button onclick="jQuery(document).ready(function($) {myFunction();});" class="dropbtn"><i class="fa fa-bars fa-lg" aria-hidden="true"></i></button>
custom.js
(function( $ ) {
function myFunction() {
document.getElementById( 'myDropdown' ).classList.toggle( 'show' );
}
window.onclick = function( event ) {
if ( !event.target.matches( '.dropbtn' ) ) {
var dropdowns = document.getElementsByClassName( 'dropdown-content' );
var i;
for ( i = 0; i < dropdowns.length; i++ ) {
var openDropdown = dropdowns[i];
if ( openDropdown.classList.contains( 'show' ) ) {
openDropdown.classList.remove( 'show' );
}
}
}
}
})( jQuery );