addEventListener vs onclick: auto executing problem RESOLVED

addEventListener vs onclick: auto executing problem RESOLVED
0.0 0

#1

I’ve just finished the Random Quote Generator project, but am trying to switch from onclick (which seems to work great) to addEventListener, and I’m not sure why it’s not working. They auto fire, without me clicking.

Caution: contains spoilers
I’m not sure how to paste my code in here, but the link to codepen is

Any help with solving the addEvent problem in changing images on click would be much appreciated.

Cheers,

Rich


#2

If you want to pass parameters to a listener function inside, addEventListener() you should be using an anonymous function or with an arrow function (ES6). Take a look at the Event listener with anonymous function and Event listener with an arrow function sections of the documentation for addEventListener().

I hope that helps!


#3

Thank you! That was it!