const parentList = document.getElementById('parentList'); const addButton = document.getElementById('addItemButton'); // Attach a single event listener to the parent element parentList.addEventListener('click', (event) => { // Check if the clicked element is an
  • if (event.target.tagName === 'LI') { console.log('Clicked item:', event.target.textContent); // Example action: Remove the clicked item event.target.remove(); } }); // Add new list items dynamically addButton.addEventListener('click', () => { const newItem = document.createElement('li'); newItem.textContent = 'New Item ' + (parentList.children.length + 1); parentList.appendChild(newItem); console.log('Added new item:', newItem.textContent); }); // Example HTML structure (for context): // //