Dynamic List Delete (Event Delegation)
JavaScript
Medium
2 views
Problem Description
You have
where each - contains text and a Delete. Items can be added later. On clicking Delete, remove that
- . Use event delegation.
Output Format
No output (DOM change).
Constraints
Assume #list exists and delete buttons have class del.
Official Solution
const list=document.getElementById('list');list.addEventListener('click',e=>{const btn=e.target.closest('button.del');if(!btn||!list.contains(btn))return;const li=btn.closest('li');if(li)li.remove();});
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!