title: How to close menu when clicking outside of the div?
body: <p>I want to be able to close a popup menu when the user clicks outside.  How can I accomplish this in pure JS?</p>
  
  <p>Here is the code I'm currently working with:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function list(){
   var r= document.getElementById('l2');
   if (r.style.height==="0px"){
              r.style.height="500px"  
          } else {
              r.style.height="0px";}
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#logo-menu img {
    width: 4vw;
    height: 2.79vw;
    border-radius: 3px;
    border: 0px solid rgba(0, 0, 0, 0.5);
    transition: 0.3s;
    z-index: 3;
    margin: 0% 0% 0% 0%;
    position: relative;
    top: 0.0vw;
    background: rgba(230, 230, 230, 1);
  }
  
  #logo-menu img:hover {
    filter: invert(100%);
  }
  
  #logo-menu img:active {
    filter: saturate(20);
    transform: scale3d(1.01, 1.01, 1.01);
  }
  
  #l2 {
    margin: -0.17% 0% 0% 0%;
  }
  
  #l2 ul {
    width: 100%;
    height: auto;
    list-style-type: none;
    padding: 0px 0px 4% 0px;
    overflow: auto;
  }
  
  #l2 li a {
    width: 20.7vw;
    height: auto;
    line-height: 1.2;
    font-weight: bold;
    font-size: 0.8vw;
    word-break: break-word;
    text-align: left;
    color: black;
    text-decoration: none;
    background: rgba(255, 255, 255, 1);
    display: block;
    margin: 3.5% 0% -2% 1.5%;
    padding: 1.5% 0% 2% 0%;
    overflow: hidden;
    border-radius: 5px;
    float: left;
    position: relative;
    left: 4%;
  }
  
  #l2 p {
    width: 100%;
    height: auto;
    position: relative;
    left: 2%;
    top: 0.5vw;
  }
  
  #l2 img {
    width: 3.5vw;
    height: 3.4vw;
    float: left;
    margin: 0.5% 0% 0% 1%;
    border-radius: 5px;
    background: rgba(255, 255, 255, 1);
  }
  
  #l2 li:hover&gt;a {
    background: rgba(0, 0, 0, 1);
    color: white;
  }
  
  #l2 li a:hover img {
    filter: invert(100%);
  }
  
  #l2 li:active&gt;a {
    background: rgba(20, 20, 20, 1);
    color: white;
  }
  
  .dropdown-content {
    background: rgba(235, 235, 235, 1);
    overflow: auto;
    width: 100%;
    float: left;
    height: 0px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="logo-menu"&gt;&lt;img src="https://png.icons8.com/metro/1600/star.png" onclick="list()" class="dropbtn" /&gt;&lt;/div&gt;
  &lt;div id="l2" class="dropdown-content"&gt;
    &lt;ul&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic1.png" /&gt;
          &lt;p&gt;list item 1&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic2.png" /&gt;
          &lt;p&gt;list item 2&lt;/p&gt;
        &lt;/a&gt;
      &lt;/li&gt;
  
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic3.png" /&gt;
          &lt;p&gt;list item 3&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic4.png" /&gt;
          &lt;p&gt;list item 3&lt;/p&gt;
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt; &lt;img src="pic5.png"&gt;
          &lt;p&gt;list item 4&lt;/p&gt;
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt; &lt;img src="pic6 .png"&gt;
          &lt;p&gt;list item 5&lt;/p&gt;
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic7 .png"&gt;
          &lt;p&gt;list item 6&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic8 .png"&gt;
          &lt;p&gt;list item 7&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic9 .png"&gt;
          &lt;p&gt;list item 8&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;a href="list.html"&gt;&lt;img src="pic10 .png"&gt;
          &lt;p&gt;list item 9&lt;/p&gt;
  
        &lt;/a&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>