Why is addEventListner not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is addEventListner not working?

Hey guys, so I saw a youtube video on 3D animation and then tried to do the same. This is my code. https://anothervatsalsharma.github.io/first3DAnimation/ The above is the link to the GitHub website where the same code works perfectly. It works perfectly in browser but when I type it in the code playground, it doesn't work. Please help. Heads up : I've used mousemove so for best experience view the above code in a laptop/pc. https://code.sololearn.com/Wa3A7a15A25A/?ref=app

29th Dec 2020, 5:59 AM
Vatsal Sharma
Vatsal Sharma - avatar
4 Answers
+ 2
Put your code in the JS tab inside a window.onload window.onload = ()=>{ ... code here ... } or inside a script tag at the bottom of the body. (between lines 28 and 29)
29th Dec 2020, 6:17 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg Now it works perfectly..... Can you please explain why window.onload was necessary. Was the js code perhaps being read before the entire html was loaded so an error was being caused?
29th Dec 2020, 6:22 AM
Vatsal Sharma
Vatsal Sharma - avatar
+ 2
The CSS tab and JS tab in the playground are injected into the head of the Html page prior to the DOM being constructed which is why those objects were null as they didn't exist.
29th Dec 2020, 6:44 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Ohk thanks
29th Dec 2020, 8:02 AM
Vatsal Sharma
Vatsal Sharma - avatar