Adding a click listener to every row of a list breaks as soon as rows are added later. With one listener on the parent and closest(), every row is covered, including new ones.
list.addEventListener("click", (event) => {
const button = event.target.closest("button[data-action]");
if (!button || !list.contains(button)) return;
const row = button.closest("li");
if (button.dataset.action === "delete") row.remove();
if (button.dataset.action === "edit") startEditing(row);
});
Clicks bubble up, so the list receives every click inside it. event.target is the innermost element that was clicked, which may be the icon inside the button rather than the button itself. closest() walks up from there, starting with the element itself, and returns the first ancestor that matches the selector, or null. That one call solves the “I clicked the icon” problem.
The list.contains() check stops a match outside the list, for example when the whole list sits inside another element with a matching button.
I like this pattern for tables and lists that a script rebuilds, because there are no listeners to re-attach and no leaks from forgotten ones.
A limit to know: events that do not bubble, like focus and blur, do not reach the parent. Use focusin and focusout instead, which do bubble. closest() itself works in every current browser.
Further reading: MDN: Element.closest(), MDN: Event bubbling