Clickoutside from selector

I want to use a live method to hide an element if the user clicks anywhere on the page outside that element. This is exactly the same as the clickoutside plugin , but with ajax elements loaded. Any ideas how to do this?

+3
source share
3 answers

Sort of

$("yourelementselector").live("click", function(){
    // your code
    return false; // prevents bubbling of event
});

$("body").click(function(){
   var yourElement = $("yourelementselector");
   if (yourElement.is(:visible))
   {
       yourElement.hide();
   }
});
+1
source

You can do it:

$("#myElement").live('click', function(){
   return false;
});
$("body").live('click', function(){
   $("#myElement").hide();
});

How it works: if you click on an element, the click event will not bubble, causing a click on the element <body>. If you click outside the element, however, it bubbles up, eventually falling into <body>that which hides your element.

0
source

Source: https://habr.com/ru/post/1739999/


All Articles