Search code examples
javascriptjquerymouseeventmousedown

jQuery: How to detect if mouse click location is outside of a div


I have this code which detects any mouse click on a website:

$(document).mousedown(function() {
    console.log("you clicked somewhere.");
});

How do I make it to only trigger an action if the clicked location is outside of #specificDiv?


Solution

  • You can use:

    $(document).mousedown(function(event) {
     var specificDiv= $("#specificDiv");
     if (specificDiv.is(event.target))
       console.log("you clicked specificDiv.");
    });