Ok. I am trying a function that counts numbers onclick. There are 2 buttons; the one that counts and the one that saves the number. When the latter is clicked the former should start counting from 0 but my code starts from the current number
let count = 1;
function increment() {
console.log("The button was clicked");
document.getElementById("count-el").innerText = count;
count += 1;
}
function save() {
document.getElementById("save-btn").innerText = count - 1;
document.getElementById("count-el").innerText = 0;
}
body {
margin: -2px 0 0 0;
zoom: .8;
}
h1, h2 {
margin: 0;
}
<h1>People entered</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn" onclick="increment()">INCREMENT</button>
<button id="save-btn" onclick="save()">SAVE</button>
So, in the save function, just update the "let" that you named "count"
function save() {
document.getElementById("save-btn").innerText = count - 1;
document.getElementById("count-el").innerText = 0;
count = 1;
}
When you save data just update your count variable..
let count = 1;
function increment() {
console.log("The button was clicked");
document.getElementById("count-el").innerText = count;
count += 1;
}
function save() {
document.getElementById("save-btn").innerText = count - 1;
document.getElementById("count-el").innerText = 0;
count = 1;
}
<h1>People entered</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn" onclick="increment()">INCREMENT</button>
<button id="save-btn" onclick="save()">SAVE</button>