InsideDarkWeb.com

How to make a script in javascript that counts the total number of divs clicked

Like the title says, I’m trying to make a function in Javascript that counts how many divs are clicked A N D tell me which ones were clicked For example:

<div>
 <div onclick="somefunction">1</div>
 <div onclick="somefunction">2</div>
 <div onclick="somefunction">3</div>
 <div onclick="somefunction">4</div>
</div>

Let’s say div 1 and 2 are clicked, how do I make a javascript function that will tell me which divs were clicked (in our case 1 and 2 were) and total number of divs that were clicked?

Stack Overflow Asked on November 15, 2021

2 Answers

2 Answers

To keep track of the amount of clicks, I would declare a public variable that keeps track of the click count and have the function increment it each time it is called.

To check which div is clicked, I would pass the number of the div as a parameter.

HTML:

<div onclick="someFunction(1)"> 1 </div>
<br>
<div onclick="someFunction(2)"> 2 </div>
<br>
<div onclick="someFunction(3)"> 3 </div>
<br>
<div onclick="someFunction(4)"> 4 </div>

Javascript:

    var clickCount = 0;
    
    function someFunction(divNum)
    {   
        ++clickCount;
        console.log("Clicks: " + clickCount);
        
        switch (divNum)
        {
        case 1:
            console.log("Clicked: " + 1);
            break;
            
        case 2:
            console.log("Clicked: " + 2);
            break;
            
        case 3:
            console.log("Clicked: " + 3);
            break;
            
        case 4:
            console.log("Clicked: " + 4);
            break;
        }
    }
    

Answered by finndg on November 15, 2021

var divsClicked="divs clicked = ";
var count = 0;
display=document.getElementById('display');
function somefunction(num){
divsClicked = divsClicked + num + ",";
count++;
display.innerHTML=divsClicked + "count =" + count;
}
<div>
 <div onclick="somefunction(1)">1</div>
 <div onclick="somefunction(2)">2</div>
 <div onclick="somefunction(3)">3</div>
 <div onclick="somefunction(4)">4</div>
</div>
<div id='display'><div>

Answered by DCR on November 15, 2021

Add your own answers!

Related Questions

how to find and remove array using jquery

1  Asked on December 13, 2020 by sultan-achmad

   

Laravel GraphQL and Sanctum – how to combine it?

0  Asked on December 13, 2020 by kamilon123s

     

How to save multiple figures built with a loop and mapplot packages in r?

1  Asked on December 13, 2020 by juan-carlos-rubio-polania

         

Pick from both sides?

2  Asked on December 13, 2020 by helloworld

       

how to return only the True values?

2  Asked on December 13, 2020 by hnakashima96

       

multiple image galleries on one page

2  Asked on December 13, 2020 by flw

     

System.MissingMethodException: Method not found?

34  Asked on December 13, 2020 by user603007

         

Use auto for only one variable with structured binding

3  Asked on December 13, 2020 by hkon-hgland

   

What does the & symbol mean here and whats going on here?

4  Asked on December 13, 2020 by aarat-chopra

   

How to get suffix of filename in javascript quickly?

2  Asked on December 13, 2020 by daniel-stephens

 

Getting date for calculation

1  Asked on December 12, 2020 by faizan-gauhar

   

Map bash table output to array

1  Asked on December 12, 2020 by jeroen-smink

         

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.