How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

You can use event listeners for clicks.


Following is the code −

 Live Demo

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link href="" rel="stylesheet" />
   <div class="divDemo">
      First Division
   <div class="divDemo">
      Second Division
   document.addEventListener('click', callEventFuncion)
   function callEventFuncion(event) {
      var div = document.querySelectorAll('.divDemo');
      var titleResult = document.querySelectorAll('.my-title');
      var result = Array.apply(0, div).find((v) => v.contains(;
      if (result) {
         console.log(" Incrementing Division Selection");
      else {
         console.log("Incrementing Element Only OutSide of DIV");

To run the above program, save the file name “anyName.html(index.html)”. Righ click on the file and select the option “Open with Live Server” in Visual Studio Code editor.


This will produce the following output on console −

Whenever we click the division then it will increment the division part otherwise it will increment outside of division.

Case 1

When user clicks on div section. Here, I have clicked two times on div section. The snapshot is as follows −

The output in console −

Case 2

Now, I am clicking outside the div section seven times. The snapshot is as follows −

This will produce the following output in console −

Published on 09-Nov-2020 08:02:07