function deleteClient(i) {
let confirmation ="";
notificationParent.classList.add("active");
notificationBtn.forEach((btn) => {
btn.addEventListener("click", () => {
confirmation = btn.dataset.content // ok or cancel
if(confirmation == "ok"){
clientsArray.splice(i, 1);
localStorage.Clients = JSON.stringify(clientsArray);
notificationParent.classList.remove("active");
}else{
notificationParent.classList.remove("active");
}
})
})
showClientsData();
}
Each time I click the button it delete delete all index
I tried many approaches but the result was the same
2
Answers
you can move the event listener outside the loop this should prevent this behavior
The issue is that in your deleteClient function, every time it is called, it adds new event listeners to notificationBtn. Thus if you click the button multiple times, multiple event listeners are added, and they all get triggered for a single click event, leading to the deletion of multiple indexes.
I think this should work.