Why wonā€™t my addEventListener work? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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