Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda