JavaScript Trying to Open/Close a div with one button

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

/* Set the width of the side navigation to 0 */
function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}

<div onclick="openNav()" class="minimenubutton"><img src="images/menu.png"/></div>

I'm looking for a way to have the function change every time I click this so that I can open my div with one button. As of right now, I have closeNav set to a button inside that div, but I'd like to do it all with just the "minimenubutton."

I'm new to Javascript so please go easy on me. I looked up toggles but couldn't figure them out.

Answer:1

You just need one function with 2 conditionals (if and else);

function togNav() {
  var nav = document.getElementById("nav");
  if (nav.style.width == '200px') {
    nav.style.width = '0';
    nav.style.opacity = 0;
  } else {
    nav.style.width = "200px";
    nav.style.opacity = 1;
  }
}
#nav {
  transition: opactity 3s ease-in;
  opacity: 0;
  cursor: pointer;
}
<p>Click under this paragraph</p>
<nav id="nav" onclick="togNav()" class="minimenubutton">
  <img src="https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" />
</nav>
Answer:2

You can also achieve this using checkbox + div if layout allows. Without Javascript at all.

input.toggler { display: none; }
input.toggler + label + div { display: none; }
input.toggler:checked + label + div { display: block; }
<input type=checkbox id=sometoggler class=toggler />
<label for=sometoggler>open/close</label>
<div>content to show / hide </div>
Answer:3
function toggleNav() {
    var el = document.getElementById("mySidenav");
    if(el.style.width == "200px"){
        el.style.width = "0px";
    } else {
        el.style.width = "200px";
    }
}

<div onclick="toggleNav()" class="minimenubutton"><img src="images/menu.png"/></div>
Answer:4

So I've set the session according to the documentation for express-session, as far as I can tell. But my cookie is not getting created! According to the documentation, all you have to do is set the ...

So I've set the session according to the documentation for express-session, as far as I can tell. But my cookie is not getting created! According to the documentation, all you have to do is set the ...

  1. express session working
  2. express session not working
  3. express-session doesn't work
  4. express session maxage not working
  5. express session destroy not working
  6. express session rolling not working
  7. express session redis not working
  8. express session cookie maxage not working
  9. iis express session not working
  10. express cookie session not working
  11. express session secure cookie not working
  12. express-mysql-session not working
  13. express js session not working
  14. passport express-session not working
  15. express-session not work

I am adding a class .error on click and now after time interval of 2 seconds I want to remove this class, but without page reload/refresh.I used delay() in jQuery as: $('#username').addClass('error')....

I am adding a class .error on click and now after time interval of 2 seconds I want to remove this class, but without page reload/refresh.I used delay() in jQuery as: $('#username').addClass('error')....

  1. if hasclass then remove class jquery
  2. add class then remove class jquery
  3. add class then remove class
  4. jquery fadeout then remove class
  5. jquery delay then remove class
  6. if has class then remove class
  7. remove class then addclass jquery
  8. jquery addclass delay then remove class
  9. jquery if hasclass then remove class
  10. jquery if hasclass then add class

I am trying make a responsive page with three div(each div is a column), where one column is sometimes hidden.What I aim to implement is: 1.When three divs are shown, width should be 100%. When one ...

I am trying make a responsive page with three div(each div is a column), where one column is sometimes hidden.What I aim to implement is: 1.When three divs are shown, width should be 100%. When one ...

  1. auto adjust width excel
  2. auto adjust width css
  3. auto adjust width in html
  4. auto adjust width of columns in excel
  5. auto adjust width excel shortcut
  6. auto adjust width of cells in excel
  7. auto adjust width
  8. auto adjust width vba

I have a task to create a node.js script that listens in a directory for new archive files to arrive and process them. I see that I can do this with fs.watch. The files are tar.gz archives, that ...

I have a task to create a node.js script that listens in a directory for new archive files to arrive and process them. I see that I can do this with fs.watch. The files are tar.gz archives, that ...

  1. node watch directory for changes
  2. node watch directory
  3. node-sass watch directory
  4. node.js watch directory for new files
  5. node fs watch directory
  6. node js watch directory
  7. node red watch directory
  8. node.js watch directory for changes
  9. node forever watch directory
  10. node.js fs.watch directory