An element can be hidden or shown based on if the mouse is clicked outside the element using two methods.
Method 1: Using the closest method:
- A mouseup event is to first checked upon the document
$(document).mouseup(function(e) {// rest code here}chevron_rightfilter_none - The closest() method is called on the target click. This method returns the first ancestor of the selected element in the DOM tree. The length property is then used on the result to find out the number of ancestors. If there are no ancestors, it means that the click was outside the element.
if($(e.target).closest(".container").length === 0) {// rest code here}chevron_rightfilter_none - The element is hidden using the hide() method.
$(".container").hide();chevron_rightfilter_none - Clicking inside the div:

- Clicking outside the div:

- A mouseup event is to first checked upon the document
$(document).mouseup(function(e) {// rest code here}chevron_rightfilter_none - The element is checked for 2 things, that the click does not land on the element by passing the is() method and the has() method with the click target.
The is() method check the current element against the specified element. The click target is passed as a parameter and the whole result is negated to essentially check if the click was outside the element.
The has() method is used to return all the elements which match at least one of the elements passed to this method. The length property is then used on the result to check if any elements are returned. If there are no elements returned, it means that the click was outside the element.
if(!container.is(e.target) && container.has(e.target).length === 0) {// rest code here}chevron_rightfilter_none - The element is hidden using the hide() method.
$(".container").hide();chevron_rightfilter_none - Clicking inside the div:

- Clicking outside the div:

- How to show/hide div element depending multiple values using Bootstrap and jQuery ?
- How to hide div element after few seconds in jQuery ?
- How to copy the content of a div into another div using jQuery ?
- How to Show and Hide div elements using radio buttons?
- How to Show and Hide div elements using Checkboxes ?
- How to hide div element by default and show it on click using JavaScript and Bootstrap ?
- How to hide/visible the backside of a rotated div element using CSS ?
- How to overlay one div over another div using CSS
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to define additional details that the user can view or hide?
- How to Display/Hide functions using aria-hidden attribute in jQuery ?
- How to Hide an HTML Element in Mobile View using jQuery ?
- How to detect when user scrolls to the bottom of a div ?
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | hide() with Examples
- How to hide the checkbox based on value in jQuery ?
- How to replace innerHTML of a div using jQuery?
- How to fadeOut and remove a div using jQuery ?
- How to animate div width and height on mouse hover using jQuery ?
Example:
<!DOCTYPE html> <html> <head> <title> How to hide a div when the user clicks outside of it using jQuery? </title> <style> .container { height: 200px; width: 200px; background-color: green; border: 5px solid black; } </style> <script src= </script> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to hide a div when the user clicks outside of it using jQuery? </b> <p>Click outside the green div to hide it</p> <div class="container" style="color:green"></div> <script type="text/javascript"> $(document).mouseup(function (e) { if ($(e.target).closest(".container").length === 0) { $(".container").hide(); } }); </script> </body> </html> |
Output:
Method 2: Checking the element if it contains the click target:
Example:
<!DOCTYPE html> <html> <head> <title> How to hide a div when the user clicks outside of it using jQuery? </title> <style> .container { height: 200px; width: 200px; background-color: green; border: 5px solid black; } </style> <script src= </script> </head> <body> <h1 style="color: green"> GeeksForGeeks </h1> <b> How to hide a div when the user clicks outside of it using jQuery? </b> <p>Click outside the green div to hide it</p> <div class="container" style="color:green"></div> <script type="text/javascript"> $(document).mouseup(function (e) { var container = $(".container"); if(!container.is(e.target) && container.has(e.target).length === 0) { container.hide(); } }); </script> </body> </html> |
Output:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

