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

102
Views
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 answers
Answer question

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 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!