I’m pretty new to writing code and I am using JavaScript and HTML in Visual Studio Code. I’ve been trying to solve the following exercise which is to create a button that counts how many times it is being pushed.
The HTML part is done, but I’m stuck in the JavaScript part.
Any advice to solve such a thing?
JavaScript
x
12
12
1
let counter
2
3
document.querySelector("#btnPush").addEventListener("click", plus)
4
5
function plus() {
6
7
counter = 0;
8
9
document.querySelector("#pCounter").innerHTML = counter + 1
10
11
}
12
Advertisement
Answer
JavaScript
1
11
11
1
let counter = 0
2
3
document.querySelector("#btnPush").addEventListener("click", plus)
4
5
function plus() {
6
7
counter++;
8
9
document.querySelector("#pCounter").innerHTML = counter
10
11
}
JavaScript
1
2
1
<button id="btnPush">button</button>
2
<p id="pCounter">0</p>