2 functions in javascript in same div

0

I have divs in my code in html (they are 3 menu bars), when I click, it transforms (as if it were an animation) into an x, however, I want it when it changes and I click it function other than the first. An "openNav ()" in the first and a "closeNav ()" in the second, does anyone know a way? Using the "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>
    
asked by anonymous 19.05.2016 / 20:01

1 answer

1

Can be easily solved by defining a global variable.

<script>
var open = true;

  function openNav() {
     document.getElementById("mySidenav").style.width = "250px";
     document.getElementById("main").style.marginLeft = "250px";
 }

 function closeNav() {
   document.getElementById("mySidenav").style.width = "0";
   document.getElementById("main").style.marginLeft = "0";
 }

 function myFunction(x) {
   x.classList.toggle("change");
   if(open){
     openNav();
     open = false;
   }else{
      closeNav();
      open = true;
   }
 }
</script>

Logic example: link

    
19.05.2016 / 20:06