0

I have a piece of code that closes a drop down menu, if you click somehwere on the document other than the opened menu itself. I would like to get rid of jQuery, but I'm not sure how to translate this code to pure javascript.

     $(document).ready(function() {
       $(document).click(function(event) {
         if (!$(event.target).closest('li.main').length) {
           if ($('li.main').is(":visible")) {
             $('#dropdown').hide();
           }
         }
       })
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li id="main" onclick="ToggleMainMenu();"><span>All categories</span>
  </li>
  <li> <a href="/item1">Item 1</a> 
  </li>
  <li> <a href="/item2">Item 2</a> 
  </li>
  <li> <a href="/item3">Item 3</a> 
  </li>
</ul>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    .closest() is not the only jquery in your code sample here. you have a lot of it in just this snippet – Cruiser Jan 10 '17 at 15:47
  • Provide complete relevant HTML (and css), this is not sufficient. – sinisake Jan 10 '17 at 16:27