Jquery click outside multiple div/elements

There are two sections targetDiv1 and targetDiv2(not nested). And another section collapsibleDiv. I need to run a function(be it collapsing the div) on clicking outside the element targetDiv2; but the function should not be triggerred when clicking targetDiv1. With the below code I'm unable to achieve this.. Any help would be great...

$(document).click(function(e) { 
    if(!$(e.target).closest('.targetDiv1').length && !$(e.target).is('.targetDiv1')) {

JSFiddle as per the answer from @Kweiss https://jsfiddle.net/vn8gmu8g/

Answers 1

  • Below should work

    $(document).click(function(e) { var target = $(e.target); if(!target.hasClass('.targetDiv1') && !target.hasClass('.targetDiv2')) { // perform you operation } });

Related Articles