javascript check if element click code example

Example 1: check if click is inside div javascript

var container = document.getElementsByClassName('container')[0];
document.addEventListener('click', function( event ) {
  if (container !== event.target && !container.contains(event.target)) {    
    console.log('clicking outside the div');
  }
});

Example 2: how to check if you click something in javascript

// get the element
const element = document.getElementById('profile_title')

// always checking if the element is clicked, if so, do alert('hello')
element.addEventListener("click", () => {
	alert('hello');
});