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

103
Visualizações
Reset a variable if input changes value

I'd like this function to reset the counter every time there's a change in the text input

This is my current JavaScript code:

let counter = 0

const button = document.getElementById('updatePic')
button.addEventListener('click', getimages)
button.addEventListener('click', function() {
  counter++;
  console.log(counter);
  if ( document.getElementById("dateUserInput").innerHTML /*reset everytime theres a new input*/ ){
    counter = 0
  }
});

Every time the button gets clicked the counter increments (counter++).
And I want the counter to reset every time the text changes in #dateUserInput input.

What's the most efficient way to achieve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Create the event listener outside of your button click. And use the "input" Event for your #dateUserInput element:

let counter = 0

const elButton = document.getElementById("updatePic");
const elInput = document.getElementById("dateUserInput"); 

elButton.addEventListener("click", () => {
  getimages();
  counter += 1;
});

elInput.addEventListener("input", () => {
  counter = 0; // reset counter
});
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