onsa
12/17/2016 - 12:17 AM

Run custom code upon click OUTSIDE a specific element

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();
    }
});