userimage
Moe

How to detect a click outside an element?

There are some HTML menus, which I would like to hide these elements when the user clicks outside the menus" area.

What could be the way to do so using jquery

$("#menuscontainer").clickOutsideThisElement(function() {
    // hide the menus
});


userimage
Moe
A click event to the document body which closes the window. Attach a separate click event to the window which stops propagation to the document body.

$('html').click(function() {
//Hide the menus if visible
});

$('#menucontainer').click(function(event){
    event.stopPropagation();
});


Warning, if using this technique, be aware of the dangers of stopping propagation.
Answer is