I have an array of items to which I have appended the same event listener, and I need to drop a specific item from the array, but I'd like to keep the event listener on it.
Of course I could add it separately, but I was wondering if there was an easy way.
Here is a pen to show that. The function that is supposed to remove the focus from the buttons after some time does not execute on the third button.
Here is the JavaScript for more clearness:
const buttons = [...document.querySelectorAll(".button")];
let dropButton;
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function () {
setTimeout(() => { buttons[i].blur(); }, 500);
});
if(buttons[i].classList.contains("drop")) {
dropButton = buttons.splice(i, 1);
i--;
}
}
Thanks for your help :)
buttonsarray, have you considered usingthisinside the event function to achieve the blur?