Skip to content
Advertisement

addEventListener to all buttons using forEach

I am trying to addEventListener to all of my buttons. This is my first time using addEventListener and it is not running the usergetNumber function which should display a random number when any button is clicked.

JavaScript

Advertisement

Answer

From top to bottom.

  1. There’s already a function usergetNumber() { ... } declaration in addEventListener(). It’s a function declaration not a callback here. Ref: Event Listener Callbacks.
  2. The closing bracket on usergetNumber() { ... is missing so therefore it isn’t declared.

Here’s a basic example. You can also just return and not console.log. Here, I’m just trying to duplicate the logic.

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