title: How to close the jQuery context menu
body: <p>I'm displaying a contextmenu in jQuery, but am unable to close it.</p>
  <p>These are the events that I am registering:</p>
  <pre class="lang-js prettyprint-override"><code>function openContextMenu(event) {
      event.preventDefault();
      $(&quot;#custom-menu&quot;)
          .css({
          top: event.pageY + &quot;px&quot;,
          left: event.pageX + &quot;px&quot;
      })
          .show(100);
      isContextMenuOpen = true;
  }
  
  function closeContextMenu(event) {
      console.log(&quot;Click called on document&quot;);
      if (isContextMenuOpen) {
          $(&quot;custom-menu&quot;).hide();
          console.log(&quot;Hide the context menu&quot;);
      }
  
      isContextMenuOpen = false;
  }
  
  function registerMenu() {
  
      $(document).on({
          contextmenu: function (event) {
              openContextMenu(event);
          },
          click: function (event) {
              closeContextMenu(event);
          }
      });
  }
  </code></pre>