I have a
Probably the easiest way to do this will be to monitor clicks on the entire document, and ignore it if it's not that element. If it is, then hide it.
(function(div) {
$(document).click(function(e) {
if (e.srcElement !== div) $(div).hide();
});
})($('div')[0]);
Edit: Derp, misunderstood, click inside should stay, otherwise hide... invert the equality check.
http://jsfiddle.net/robert/QcPx4/
Toggle a flag on popup hover:
var $pop = $('#popup'),
notHov = 1; // Hover flag
$pop.hover(function(){ notHov^=1; }); // Toggle flag on hover
$(document).on('mouseup keyup', function( e ){
if(notHov||e.which==27) $pop.fadeOut();
});
Note:
if somewhere on the page you have an element the prevents the mouseup event to bubble up the DOM tree reaching document (in order to be registered), you might want to create a full-screen (like an page-overlay) popup wrapper to register the click events, changing in the code above just the selector: instead of $(document) would be $('#popupWrapper')