-1

I have a very simple html code that has only one button tag. I need to count the number of mouse clicks on the button using javascript. Here is a screenshot of the Javascript code in VScode

5
  • 2
    Please edit your question and copy/paste your code. It's much simpler for us to work with code then with images Commented Jun 12, 2021 at 15:46
  • 1
    Please post the code here so that it'd be easy to copy and execute. Commented Jun 12, 2021 at 15:47
  • Does this answer your question? Count clicks on element with JavaScript Commented Jun 12, 2021 at 15:52
  • I have also found it, but it is not with event listener Commented Jun 12, 2021 at 15:53
  • 1
    Images of code are not acceptable. Commented Jun 12, 2021 at 16:03

2 Answers 2

0

Well, the code below can implement count on click functionality.

let clicks = 0;
document.querySelector('.btn').addEventListener('click', e => {
clicks++;
document.querySelector('.clicks').textContent = clicks
})
.btn {
    border: none;
  outline: none;
  background-color: purple;
  padding: 1rem 90px 1rem 2rem;
  position: relative;
  border-radius: 8px;
  letter-spacing: 0.7px;
  background-color: #5086bd;
  color: #fff;
  font-size: 21px;
  font-family: "Lato", sans-serif;
  cursor: pointer;
  box-shadow: rgba(0, 9, 61, 0.2) 0px 4px 8px 0px;
  
}
<button class="btn"> Click me </button>
<div>
  <p>You have clicked the button <span class="clicks"> 0</span> times</p>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Why not this?

let numberOfClicks = 0;
function addclick() {
  numberOfClicks++;
  document.getElementById('clicks').innerHTML = numberOfClicks;
}

document.getElementById("clickme").addEventListener("click", addclick);
<button id='clickme'>Click me!</button>
<p id='clicks'>0</p>

Update: Solution with event listener

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.