I'm facing a challenge with an array of items that have the same event listener attached to them. I need to remove a specific item from the array while preserving the event listener on it.
Adding the event listener separately is an option, but I was hoping for a more streamlined solution.
Check out this pen for a demonstration. The code snippet in question fails to remove focus from the third button as intended.
To provide further clarity, here is the relevant JavaScript code:
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--;
}
}
Your assistance is greatly appreciated :)