I am using this code:
$(\'body\').click(function() {
$(\'.form_wrapper\').hide();
});
$(\'.form_wrapper\').click(function(event){
event.stopPropagatio
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:
$(function() {
$('body').on('mouseup', function() {
$('#your-selector').hide();
});
});
if you have trouble with ios, mouseup is not working on apple device.
does mousedown /mouseup in jquery work for the ipad?
i use this:
$(document).bind('touchend', function(e) {
var container = $("YOURCONTAINER");
if (container.has(e.target).length === 0)
{
container.hide();
}
});
Attach a click event to top level elements outside the form wrapper, for example:
$('#header, #content, #footer').click(function(){
$('.form_wrapper').hide();
});
This will also work on touch devices, just make sure you don't include a parent of .form_wrapper in your list of selectors.
You'd better go with something like this:
var mouse_is_inside = false;
$(document).ready(function()
{
$('.form_content').hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").mouseup(function(){
if(! mouse_is_inside) $('.form_wrapper').hide();
});
});
And for Touch devices like IPAD and IPHONE we can use following code
$(document).on('touchstart', function (event) {
var container = $("YOUR CONTAINER SELECTOR");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
This code detects any click event on the page and then hides the #CONTAINER
element if and only if the element clicked was neither the #CONTAINER
element nor one of its descendants.
$(document).on('click', function (e) {
if ($(e.target).closest("#CONTAINER").length === 0) {
$("#CONTAINER").hide();
}
});