Why won’t my addEventListener work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why won’t my addEventListener work?

https://code.sololearn.com/W60ks5SBnd88/?ref=app I’m using document.execCommand to create HTML when a button is pressed. Within the HTML created, is a class that I’m trying to access so I can create a usable button by pressing the first button. But I can’t do this because the addEventListener won’t work with the class created and I’m not sure why. This is on line 42. If anyone has any tips or info they can offer, that would be much appreciated.

3rd Dec 2018, 9:59 PM
Thomas Czernek
Thomas Czernek - avatar
2 Answers
+ 1
It was a scope problem. The addEventListner needed to be within the function where the HTML was being generated.
3rd Dec 2018, 11:03 PM
Thomas Czernek
Thomas Czernek - avatar
+ 2
FYI, just in case this is useful function handleClick(event){ alert(event); // object MouseEvent } window.onload = function(){ document.querySelector("#clickme").addEventListener("click", handleClick, false); }
3rd Dec 2018, 11:23 PM
Kirk Schafer
Kirk Schafer - avatar