Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

detect a click outside an element javascript

$(document).click((event) => {
  if (!$(event.target).closest('#element').length) {
    // the click occured outside '#element'
  }        
});
Comment

handle click outside the element

const onClickOutside = (element, callback) => {
  document.addEventListener('click', e => {
    if (!element.contains(e.target)) callback();
  });
};

onClickOutside('#my-element', () => console.log('Hello'));
// Will log 'Hello' whenever the user clicks outside of #my-element
Comment

how to detect click outside div

$(window).click(function() {
  //Hide the menus if visible
});

$('#menucontainer').click(function(event){
  event.stopPropagation();
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to use graphql api in react 
Javascript :: react buffer to image 
Javascript :: js entries 
Javascript :: react check internet connection 
Javascript :: nodejs return value 
Javascript :: google map react search place 
Javascript :: nestjs allow origin 
Javascript :: how to make a timer in javascript 
Javascript :: button change slider value js 
Javascript :: model mongoose 
Javascript :: react html parser 
Javascript :: react hello world 
Javascript :: js string interpolation 
Javascript :: post request with authorization 
Javascript :: how to push key value pair to object javascript 
Javascript :: react sign in with linkedin 
Javascript :: angular ionic capacitor nfc reader 
Javascript :: html js how to draw on screen 
Javascript :: jquery remove multiple class 
Javascript :: how to clear input value in antdesign form on submit 
Javascript :: javascript new date invalid date dd/mm/yyyy 
Javascript :: javascript change color 
Javascript :: js fetch 
Javascript :: css variable value changing with javascript 
Javascript :: extract from a string in javascript 
Javascript :: resize image in node.js 
Javascript :: loop through nested json object typescript 
Javascript :: ant design charts 
Javascript :: jquery multiple ids same funjquery apply function to multiple elementsction 
Javascript :: javascript async/await 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =