jquery - Side menu (help), onClick() opens (but I want it to close if one clicks else where outside the menu -


on click menu opens problem stays open, want closed when user clicks outside menu. copy code , save html file , try run try it, if it, keep it. if there confusing, ask me, i'll answer i. sooooooooooo annoying!~ stackoverflow not letting post simple question,

$(document).ready(function () {              $("#itemtree").click(function () {                  $("#subcontainer").toggleclass("subcontainer");                  $("#disabled").toggleclass("disabled")              });          });
p {              color: #fff;              padding-left: 10px;          }            .sidecontainer {              background-color: #676464;              width: 28px;              height: 100%;              position: fixed;              border-right: 5px solid #676464;          }            .subcontainer {              background-color: #333;              width: 300px;              height: 100%;              margin-left: 28px;              position: absolute;          }            .vtext {              transform: rotate(90deg);              color: #fff;              margin-top: 50px;              position: absolute;              font-weight: bold;              left: -10px;              letter-spacing: 3px;          }            .itemtree {              background-color: #808080;              position: absolute;              height: 200px;              width: 100%;              border-left: 5px solid #ff6a00;              cursor: pointer;          }                .itemtree:hover {                  border-left: 5px solid #0094ff;              }            .disabled {              display: none;          }
<!doctype html>  <html lang="en">  <head>      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">        </head>  <body>      <div class="sidecontainer">          <div id="itemtree" class="itemtree">              <span class="vtext">tree</span>          </div>          <div id="subcontainer" class="">              <div id="disabled" class="disabled">                  <div class="container-fluid">                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                      <p> type of content.</p>                  </div>              </div>          </div>      </div>        <div style="height: 1600px; width: 20px;"></div>  </body>  </html>

add click event document , check if target (the element triggers click event) child of subnavigation.

$(document).on('click', function (ev) {   if (!$(ev.target).closest('#subcontainer').length) {     $("#subcontainer").removeclass("subcontainer");     $("#disabled").removeclass("disabled");   } }); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -