0
I have Ivs in my html code (there are 3 menu bars), when I click, it turns (as if it were an animation) into an x, however, I want when it turns and I click on it to perform another function other than the first one. An "openNav()" in the first and a "closeNav()" in the second, does anyone know a way? Using "onClick="
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.getElementById("t").onClick="closeNav()";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
function myFunction(x) {
x.classList.toggle("change");
}
<div id="t" class="container1" onclick="openNav()">
<div class="container1" onclick="myFunction(this)" >
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
</div>
It gives this error "Uncaught Typeerror: openNav is not a Function" despite being just before
– Pedro Freitas
@Pedrofreitas I think because the variable and Function have the same name. I changed and exemplified with an example.
– Matheus
Thanks! That’s right, thank you very much
– Pedro Freitas
Global variables are a bad solution. Use element attributes to make manipulation or encapsulate this in a
IIFE
. A solution without global variables: https://jsfiddle.net/u4rqyfxz/1/– Gabriel Katakura