Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda