We can use jQuery mouseup() method to check the user click on the document.
This is how we can do that.
In the example above we check if the target of the click is neither the container nor a descendant of the container and if so, we will hide the element.
Discussion
jQuery mouseup() method
This method binds an event handler to the “mouseup” JavaScript event. It triggers that event on an element specified.
This method is a shortcut for .on(‘mouseup’, handler) and .trigger(‘mouseup’).
The mouseup event is sent to an element when the mouse pointer is over the element, and the mouse button is released.
Example
mouseup
mouseup and mousedown
Sources: jQuery Official Guide and Stakeoverflow
