Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
How to get the counter to start from 0 again in JavaScript

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>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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;
}
about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!