title: How to show hidden div onclick of button and hide when not in focus using JS
body: <p>I'm trying to create add a way to show a div onclick of a button and hide it when not in focus / when user clicks outside the div.</p>
  <p>I've already managed to add a button that can show a hidden div, but I can't figure out how to make it hidden again when focus is lost.</p>
  <p>I've read this: <a href="https://stackoverflow.com/questions/5337828/hide-a-div-when-it-loses-focus-blur">Hide a DIV when it loses focus/blur</a></p>
  <p>...and other articles, but I couldn't follow them to make it work..</p>
  <p>Please see my code so far:</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 openCardsList() {
    let window = document.getElementById("anchor-cards-list");
    window.style.display = "block";
  }
  $(document).not("#anchor-cards-list").click(function() {
    $('#anchor-cards-list').hide();
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"&gt;
  
  &lt;div class="collapse" id="anchor-cards-list"&gt;Lorem Ipsum&lt;/div&gt;
  
  &lt;button id="anchor-open-cards-list-btn" onclick="openCardsList();"&gt;Collapse Maincard&lt;/button&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Any help would be appreciated. Thank you in advance!</p>