Hello! Why I can't use here in Sololearn sandbox JavaScript method "addEventListener" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hello! Why I can't use here in Sololearn sandbox JavaScript method "addEventListener"

addEventListener bug https://code.sololearn.com/Wo2yP85pyXoi/?ref=app

19th Jan 2019, 11:58 AM
Istamal Dagalaev
Istamal Dagalaev - avatar
3 Answers
+ 8
It does only that you need to add window.onload = function(){ //code goes here } Like in this code: https://code.sololearn.com/Wj153VKGavW0/?ref=app
19th Jan 2019, 12:36 PM
Uni
Uni - avatar
+ 5
Javascript needs all html dom elements loaded before doing something with them. Enclosing your code like this: window.onload = function() { // your original javascript code } It means, when all html elements are created and loaded in my browser, I could reference them (adding event listeners, etc...)
19th Jan 2019, 12:47 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 3
You have to wrap the code with window.onload. Because the JS is running before the DOM or window is loaded. Solution: https://code.sololearn.com/Wyxg2YXi8qG0/#js
19th Jan 2019, 12:42 PM
Niush
Niush - avatar