Software/Scripts How to Hide Div when Click Outside of the Element using jQuery!

emailx45

Social Engineer
Joined
May 5, 2008
Messages
2,387
Reaction score
2,149
How to Hide "Div" when Click Outside of the Element using jQuery
[SHOWTOGROUPS=4,20]
Nov 23, 2018

Hide element on click outside, is a must-have functionality for the dropdown menu. Apart from that, it also used in some situations where you need to hide div when the user clicks outside of this element.

You can easily hide div or element when click outside of it using jQuery.

In the example code snippet, we will show how to hide element on click outside using jQuery. The div will be hidden when the user clicks on outside of this element.

Use jQuery mouseup event (.mouseup()) with target property (event.target) to detect click event and hide div when clicking outside of the specific element.

Code:
<script>
$(document).mouseup(function(e){
var container = $("#elementID");

// If the target of the click isn't the container
if(!container.is(e.target) && container.has(e.target).length === 0){
container.hide();
}
});
</script>


[/SHOWTOGROUPS]
 

I am so good

Bin Dropper
Brute Forcer
Basic Carder
BIN Hunter
CVV Seller
Black Hat
Doxxer
Joined
Jul 6, 2024
Messages
5,122
Reaction score
1,421
Escrow Deals
36
"Dude, you can use the `blur()` method to hide the div when the user clicks outside of the element. Here's a simple code snippet that should do the trick: `$(document).click(function(e) { if (!$(e.target).is('#div_id')) { $('#div_id').hide(); } });`"
 

alina22222

New member
Joined
May 12, 2011
Messages
2
Reaction score
0
"Hey, you can use the `mouseleave` event combined with the `mouseout` event on the parent element to achieve this. For example, `$('#div').mouseleave(function() { $('#div').hide(); });` This will hide the div when you move your cursor away from it."
 

Forsnw

Member
Joined
Oct 6, 2006
Messages
5
Reaction score
0
"Dude, have you tried using the blur() function in jQuery? It automatically hides the div when you click outside of it, no need for any extra js code."
 

DHarry

New member
Joined
Nov 23, 2011
Messages
4
Reaction score
0
"Hey, just wanna share my 2 cents. You can use the `mouseleave` event in jQuery to achieve this, something like `$('#your_element').on('mouseleave', function() { $(this).hide(); });`. Make sure to bind it to the specific element you want to hide the div for."
 

diik

New member
Joined
Jul 24, 2011
Messages
2
Reaction score
0
"Hey, I had this same issue once and the solution I found was using the `blur()` function on the element. So, when the user clicks outside of the div, just trigger the `blur()` event on the div itself. Works a charm!"
 
Top