I know that this question was answered, And all the answers are good. But I wanted to add my two cents to this question for people who have a similar (but not quite the same) problem. Adils answer is really good. More generally, we can do something like this:
$('body').click(function(evt){ if(!$(evt.target).is('#menu_content')) {
Thus, we not only handle events that are triggered by anything other than an element with id = "menu_content", but also we can handle events that are triggered by anything except any element that we can select using CSS selectors. for example, in the following code snippet, I get events triggered by any element except all li elements that are descendants of a div element with id = "myNavbar"
$('body').click(function(evt){ if(!$(evt.target).is('div#myNavbar li')) {
gandalf Sep 05 '16 at 9:52 2016-09-05 09:52
source share