JavaScript count number of clicks

Last Updated On Friday 10th Jun 2022

JavaScript count number of clicks.

  • Counting the amount of clicks on an element is among the simplest methods you can perform using JavaScript.
  • You need to do is to declare a variable that will keep track of your clicks and then use the addEventListener method on your target element.
  • When user clicks the button, the count value increased by 1 and display it on the screen.

html count clicks

Create an HTML button element with the class of btn.

number of click

	<button class="btn">Tap Me</button>
	
  • Use the querySelector() method to select the .btn class on the button element.
	let btn = document.querySelector(".btn");
	
  • Then create the variable called count to store our future clicks. We give it a default value of .
	let count = 0;
	
  • Then use the addEventListener().
	btn.addEventListener("click", function() {
  count += 1;
  console.log(count);
});
	
  • Within our function, we specify that want to add 1 to our count variable everytime the click event is triggered. To add 1, we use the += (plus equals) operator.

Count how many times a button is clicked javascript

	let btn = document.querySelector(".btn");

let count = 0;

btn.addEventListener("click", function() {
  count += 1;
  console.log(count);
});
	

Here’s a CodePen with Count Clicks & Increase

References

javascript count number of clicks
count clicks javascript
count number of clicks javascript
how to count number of clicks in javascript
count how many times a button is clicked javascript
javascript count clicks
how to count clicks in javascript
javascript count function
click count javascript
count the clicks