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.
jQuery mouseup() method
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.
mouseup and mousedown
Sources: jQuery Official Guide and StakeoverflowaddEventListener event mouseup onclick