1var ignoreClickOnMeElement = document.getElementById('someElementID');
2
3document.addEventListener('click', function(event) {
4 var isClickInsideElement = ignoreClickOnMeElement.contains(event.target);
5 if (!isClickInsideElement) {
6 //Do something click is outside specified element
7 }
8});
1$(document).click((event) => {
2 if (!$(event.target).closest('#element').length) {
3 // the click occured outside '#element'
4 }
5});
1// Vanilla js
2var ignoreMe = document.getElementById("ignoreMe");
3window.addEventListener('mouseup', function(event){
4 if (event.target != ignoreMe && event.target.parentNode != ignoreMe){
5 // Place your output
6 }
7});
1// jQuery
2$(window).click(function() {
3//Hide the menus if visible
4});
5
6// JavaScript
7const getBody = document.querySelector('body');
8getBody.addEventListener('click', () => {
9 //Hide the Menus if Visible
10});
11
12// Stop Propogation on Element you want to hide
13$('#menucontainer').click(function(event){
14 event.stopPropagation();
15});
16
17// Example
18menu.addEventListener('click', (event) => {
19 event.stopPropagation();
20 // Open Menu
21});