$(document).click(function(event){ if (event.target !== $('#nav')) { $('#nav').removeClass('open'); $('#navIcon').removeClass('fa-times'); $('#navIcon').removeClass('fa-align-left'); }else{ event.stopPropagation(); } }); 

This code should close the menu if the purpose of the click is not a block with a menu. But if the click on this particular block then the function stops, But after writing it, the menu does not open at all.

    2 answers 2

    Try using .not ()

    • Does not work with .not() - vitalii

    What does it mean?

    function stops


      if (event.target !== $('#nav')) { 

    event.target - DOM element, $('#nav') - returns a dynamically generated jQuery wrapper object. Naturally, they cannot be equal.

      if (event.target.id != 'nav' && $(event.target).closest("#nav").length == 0) {