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?
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
});