MutterCorp
MutterCorpNotice · Notícias
Programação·

Event Delegation in JavaScript: Stop Adding Listeners to Every Element

If you have ever built a to-do list, a comment section, or a product grid, you have probably written a loop that adds a click listener to every single button. It works — until the list grows, or new items get added later and their buttons do nothing. Event delegation fixes both problems with one listener. The idea is simple. Events in JavaScript bubble up. When you click a button inside a list, the click event travels up through the list, the container, and the body. So instead of listening on…

Publicado no Notice. Categoria: Programação.

Notícias sobre o mesmo assunto em outras fontes