Help me get my click button to log clicks!

Hey all,

I’m very very new to JavaScript. One thing I’m trying to do is log the number of times I click a button. Here is my code:

Specifically the last line in my JavaScript code is where I’m having trouble. When I click the green button, I want it to count each time that I click. I can get it to go from my html statement to 1 but I can’t get it to go up from there.

I believe I’m very close but missing something. Even though it’s text content, it seems to be logging as a number but I can’t get it to increase beyond 1.

Hope that makes sense. (also my syntax needs practice so I’ll keep trying to clean that up).

Currently, when you click the green button, the following line executes:

let clicked = (document.querySelector(".num2").textContent = 1);

You assign the number 1 to the text of the div with class=“num2” and assign the div’s text value to clicked. At this point, clicked is equal to 1. The next line:


increments clicked by 1 so it is now 2.

Everytime you click on the green button clicked will always end up being 2 at the end because you always assign it the value 1 and increment it by 1 to be 2.

What I think you want is to display the incremented value each time you click on the green button.

There are several ways to do it, but the easiest would be to initialize clicked to 0 outside the click event listener for the green button and then inside the event listener, just increment clicked by 1 and then assign clicked to the textContent property of the div with class=“num2”.

Ahhh - Thank you so much!!! There were points where I was assigning ‘clicked’ outside the event listener but then it wouldn’t do anything - My issue was that I never assigned ‘clicked’ to the textContent property f ‘num2.’ I was always assigning that either a 0 or 1.

Thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.