tags:

views:

30

answers:

3

I want to use the live method to hide an element if the user clicks anywhere on the page outside of that element. It is exactly like what clickoutside plugin does, but with ajax loaded elements. Any ideas on how to do this?

+2  A: 

Something like

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

$("body").click(function(){
   var yourElement = $("yourelementselector");
   if (yourElement.is(:visible))
   {
       yourElement.hide();
   }
});
rahul
Care to explain the reason for the down vote??
rahul
A: 

You can do this:

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

How it works: If you click on the element, the click event doesn't bubble up, causing a click on the <body> element. If you click outside the element though, it bubbles up, eventually getting to <body> which hides your element.

Nick Craver
A: 

There is an example a modified version of the clickoutside event that works with live: http://benalman.com/news/2010/03/jquery-special-events/#highlighter_680190

PetersenDidIt