'How can fix this code? button on click will give 1 point & another that remove 1
I have used
<button onclick="addPoint()>
var p = document.getElementById("p");
var total = 0;
function addPoint() {
total++
}
function minusPoint() {
total--;
}
p.innerHTML = total;
Solution 1:[1]
You need to update element value p.innerHTML = total; inside the click handler as below
var p = document.getElementById("p"); var total = 0;
function addPoint() {
total++;
p.innerHTML = total;
}
function minusPoint() {
total--;
p.innerHTML = total;
}
<button class="button" onclick="addPoint()">add a point</button> <button class="button" onclick="minusPoint()">remove a point</button> <p id="p">Points:</p>
Solution 2:[2]
Using innerHTML to update the total every time the buttons are clicked.
var p = document.getElementById("p");
var total = 0;
function addPoint() {
total++;
p.innerHTML = total;
}
function minusPoint() {
total--;
p.innerHTML = total;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sumeet kumar |
| Solution 2 | Jonathan Wang |
