0

I've got small menu, which I want to automatic close on clicking anywhere on the page. I know it's probably a matter of adding one line of code, but everything I've tried so far doesn't work...

Now it is simply js function:

  document.getElementById("mySidenav").style.width = "250px";
}

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

Edit: sorry, Sorry, I didn't know I could edit the first post... Here's the rest of the code:

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

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

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 99999;
  top: 0px;
  right: 0px;
  background-color: #111;
  overflow-x: hidden;
  overflow-y: auto;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

@media screen and (max-height 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <h2>Position #1</a>
  </h2>
  <h2>Position #2</a>
  </h2>
  <h2>Position #3</a>
  </h2>
  <h2>Position #4</a>
  </h2>
  <h2>Position #5</a>
  </h2>
  <h2>Position #6</a>
  </h2>
  <h2>Position #7</a>
  </h2>
  <h2>Position #8</a>
  </h2>
  <h2>Position #9</a>
  </h2>
  <h2>Position #10</a>
  </h2>
</div>
<span style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 99999;
  top: 0px;
  right: 0px;
  background-color: #111;
  overflow-x: hidden;
  overflow-y: auto;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

@media screen and (max-height 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
</head>
<body>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<h2>Position #1</a></h2>
<h2>Position #2</a></h2>
<h2>Position #3</a></h2>
<h2>Position #4</a></h2>
<h2>Position #5</a></h2>
<h2>Position #6</a></h2>
<h2>Position #7</a></h2>
<h2>Position #8</a></h2>
<h2>Position #9</a></h2>
<h2>Position #10</a></h2>
</div>

<span style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>

<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
</script>

</body>
</html>
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
vicxo
  • 1
  • 1

2 Answers2

3

To hide an element you should use style.display = "none":

function closeNav() {
  document.getElementById("mySidenav").style.display = "none";
}

To show it again, put the display back to what is was. A lot of the time it's block:

function openNav() {
  document.getElementById("mySidenav").style.display = "block";
}

In your case, since you don't provide enough code I'm going to assume you also need to know where to call your closeNav function. You could do that on the document:

document.onclick = function() {
    closeNav();
}

And here's the solution based on the code you provided:

document.onclick = function(evt) {
  if (!event.target.matches("#menu")) {
    closeNav();
  }
}

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

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

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 99999;
  top: 0px;
  right: 0px;
  background-color: #111;
  overflow-x: hidden;
  overflow-y: auto;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

@media screen and (max-height 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <h2>Position #1</h2>
    <h2>Position #2</h2>
    <h2>Position #3</h2>
    <h2>Position #4</h2>
    <h2>Position #5</h2>
    <h2>Position #6</h2>
    <h2>Position #7</h2>
    <h2>Position #8</h2>
    <h2>Position #9</h2>
    <h2>Position #10</h2>
  </div>

  <span id="menu" style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>


</body>

</html>
Word Rearranger
  • 1,306
  • 1
  • 16
  • 25
0

You can try

function closeNav() {
  document.getElementById("mySidenav").style.display= "none";
}
Srimoyee Sen
  • 181
  • 9