Run custom code upon click OUTSIDE a specific element
$(document).mouseup(function(element) {
var container = $(containerSelector);
if (!container.is(element.target) // if the target of the click isn't the container...
&& container.has(element.target).length === 0) // ... nor a descendant of the container
{
// run custom code, e.g. hide element
container.hide();
}
});