#development #frontend #javascript #typescript #vuejs

Sometimes I need to detect whether a click happens inside or outside of a particular element. This is one approach:

 1window.addEventListener('mousedown', e => {
 2  // Get the element that was clicked
 3  const clickedEl = e.target;
 4
 5  // `el` is the element you're detecting clicks outside of
 6  if (el.contains(clickedEl)) {
 7    // Clicked inside of `el`
 8  } else {
 9    // Clicked outside of `el`
10  }
11});