
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?
You can use event listeners for clicks.
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" /> <body> <div class="divDemo"> First Division </div> <div class="divDemo"> Second Division </div> </body> <script> 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(event.target)); if (result) { console.log(" Incrementing Division Selection"); } else { console.log("Incrementing Element Only OutSide of DIV"); } } </script> </html>
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.
Output
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 −
- Related Questions & Answers
- How IllegalArgumentException automatically handled inside 'if' condition in java?
- How to click the 'Ok' button inside an alert window with a Selenium command?
- How to get attribute of an element when using the 'click' event in jQuery?
- How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?
- How to make 'Paneer' at home?
- How to access 'this' keyword inside an arrow function in JavaScript?
- How to make pylab.savefig() save image for 'maximized' window instead of default size in Matplotlib?
- Selenium WebDriver Error: AttributeError: 'list' object has no attribute 'click'
- MongoDB query with an 'or' condition?
- ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'localhost'?
- How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
- How to make numbers 'human readable' in MySQL?
- How to use multiple click event on HTML5 canvas?
- How to iterate over dictionaries using 'for' loops in Python?
- Program to find number of string we can make where 'a' can be 'a' or 'b', and 'b' remains 'b'in Python
Advertisements