Skip to content
Advertisement

How to add an event listener to all items in array

so this is noobie .. but i am trying to complete a challenge with more due diligence than just downloading the answer,

my current html code is:

JavaScript

my javascript codes :

JavaScript

essentially, the current code is running the event listener for the first and last item of the array only, trying to add it to all 6 but am stuck

thanks to all

Advertisement

Answer

Instead of attaching listeners to each button, add one to the container (set) and, using event delegation, let that listener capture all the events that “bubble up” the DOM from its child elements, and call a function.

JavaScript
JavaScript

Additional documentation

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement