Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Handle Event Propagation and Delegation | Event Handling and User Interactions in JavaScript
Advanced JavaScript Mastery

bookChallenge: Handle Event Propagation and Delegation

Task

You're building a dynamic list where users can add and delete specific items.

  1. Use event delegation by adding a single click event listener to the ul with ID dynamic-list;
  2. When an item is clicked:
    • If the clicked element is an <li>, show an alert with the text of the clicked item;
    • If the clicked element is a "Delete" button, remove the corresponding <li> item from the list.
index.html

index.html

index.css

index.css

index.js

index.js

copy
  • Use event.target.tagName === 'LI' to check if the clicked element is an <li>;
  • Use event.target.classList.contains('delete-btn') to check if the clicked element is a "Delete" button.
index.html

index.html

index.css

index.css

index.js

index.js

copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 2.22

bookChallenge: Handle Event Propagation and Delegation

Scorri per mostrare il menu

Task

You're building a dynamic list where users can add and delete specific items.

  1. Use event delegation by adding a single click event listener to the ul with ID dynamic-list;
  2. When an item is clicked:
    • If the clicked element is an <li>, show an alert with the text of the clicked item;
    • If the clicked element is a "Delete" button, remove the corresponding <li> item from the list.
index.html

index.html

index.css

index.css

index.js

index.js

copy
  • Use event.target.tagName === 'LI' to check if the clicked element is an <li>;
  • Use event.target.classList.contains('delete-btn') to check if the clicked element is a "Delete" button.
index.html

index.html

index.css

index.css

index.js

index.js

copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
some-alt