2017  Kodetalk | Feedback | Privacy Policy | Terms | About

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

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


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