check if click is inside div javascript 1

check if click is inside div javascript

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

Here is what the above code is Doing:
1. We are getting the container div by using the getElementsByClassName method.
2. We are adding an event listener to the document object.
3. We are checking if the container div is not the target of the event and if the container div does not contain the target of the event.
4. If the above condition is true, we are logging a message to the console.

Similar Posts