I have a button which I want to toggle between two 'functionalities.' I want it to toggle my menu open and close
I want to add onclick="openNav()" on first click and then onclick="closeNav()" on the second
html
<button id="nav-icon" class="navbar-toggler hidden-sm-up" type="button" onclick="openNav()"></button>
I'm not sure the best way to do this, the code works if they are on two separate buttons.
EDIT: I am using Wordpress so all the javascript is in a separate .js file brought in
You can change onclick of the element at end of both functions.
let btn = document.getElementById('nav-icon');
function openNav(){
console.log('openNav called');
btn.onclick = closeNav
}
function closeNav(){
console.log('closeNav called');
btn.onclick = openNav
}
<button id="nav-icon" class="navbar-toggler hidden-sm-up" type="button" onclick="openNav()"></button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With