Created
April 10, 2018 05:47
-
-
Save yayMark/408ad47598d7ff7191575ea39b877dbf to your computer and use it in GitHub Desktop.
JavaScript/jQuery: Detect a click outside an element. Used for deactivating a drop down menu.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//detect click outside http://bassta.bg/2013/08/detect-click-event-outside-element/ | |
var everything = jQuery(window); | |
var menuBody = jQuery(".menu-body"); | |
var menu = jQuery(".menu"); | |
var menuButtonClass = 'mobile-menu-button-img'; | |
everything.on("click.yayMark", function (event) { | |
var menuButtonClicked = event.target.className == menuButtonClass; | |
if (menuBody.has(event.target).length == 0 | |
&& !menuBody.is(event.target) | |
&& !menuButtonClicked) { | |
if (menu.css('display') == 'block') { | |
menu.toggle(); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment