javascriptjquerymouseeventdom-eventsunobtrusive-javascript

Detecting if click is not some specific divs


I am developing a sort of horizontal menu but visible on click and hide on click but visible when click is on some arrow while hide when click is on document but not some div container. In that div container there are also some other elements. If we write document.onclick=closingfunction then it close whether user click in that div itself. I want to call closefunction when user click anywhere but not in that div itself or on arrow.

It seems that there will be its solutions, but I haven't found one, so that's why posting it here.


Solution

  • You can use jQuery is() function to detect if current target matches specific selector:

    $('ul.container li').click(function(e){
        if ($(e.target).is('.dropdown')) {
            //do something when clicked inside dropdown
        }
    })