I am using this code:
$('body').click(function() {
$('.form_wrapper').hide();
});
$('.form_wrapper').click(function(event){
event.stopPropagation();
});
And this HTML:
<div class="form_wrapper">
<a class="agree" href="javascript:;">I Agree</a>
<a class="disagree" href="javascript:;">Disagree</a>
</div>
The problem is that I have links inside the DIV and when they no longer work when clicked.
Built off of prc322's awesome answer.
This adds a couple things...
I hope this helps someone!
p
is the element name. Where one can pass the id or class or element name also.Live DEMO
Check click area is not in the targeted element or in it's child
UPDATE:
jQuery stop propagation is the best solution
Live DEMO
So many answers, must be a right of passage to have added one... I didn't see a current (jQuery 3.1.1) answers - so:
(Just adding on to prc322's answer.)
In my case I'm using this code to hide a navigation menu that appears when the user clicks an appropriate tab. I found it was useful to add an extra condition, that the target of the click outside the container is not a link.
This is because some of the links on my site add new content to the page. If this new content is added at the same time that the navigation menu disappears it might be disorientating for the user.