var n = 0; $("#container").mouseenter(function() { n = 0; }).mouseleave(function() { n = 1; }); $("html").click(function(){ if (n == 1) { alert("clickoutside"); } });