Sergio del Amo Sergio del Amo - 10 months ago 54
Javascript Question

How do I detect a click outside an element?

I have some HTML menus, which I show completely when a user clicks on the head of these menus. I would like to hide these elements when the user clicks outside the menus' area.

Is something like this possible with jQuery?

$("#menuscontainer").clickOutsideThisElement(function() {
// Hide the menus

Answer Source

Attach 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.

$(window).click(function() {
//Hide the menus if visible