Uncaught TypeError: Cannot read property 'style' of null Line 33 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Uncaught TypeError: Cannot read property 'style' of null Line 33

Hi so I'm trying to animate a box. But the console says the error thats in the title. In in JS. HTML seems to be correct, no problem. It's the line33 in my code Here's the code : var pos = 0; var box = document.getElementById("box"); var t = setInterval (move, 10); function move () { if (pos >= 150) { clearInterval(t); } else { pos += 1; box.style.left = pos+"px"; } } Line 33 is the "box.style.left = pos+"px";" Someone who knows, pls help me understand what's wrong here. Thanks Btw here's the code if you wanna see it live. >> : https://code.sololearn.com/Wz3rQNMXDNwP/?ref=app

14th Nov 2020, 4:34 PM
Aravind-R-C 06
Aravind-R-C 06 - avatar
2 Answers
+ 6
AravindR2006 , just add in the beginning of JS window.onload =(), you need first load all html before you use it. Look at the code. https://code.sololearn.com/WsIznFyZ03gX/?ref=app
14th Nov 2020, 4:45 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
Allright TheWh¡teCat 🇧🇬 The animation works! Thank you!! But there's another problem. Sorry if I'm troubling but can u look at the code again at my profile because the button isn't working. Says "show ()" isn't defined in line 26 of HTML.
14th Nov 2020, 4:54 PM
Aravind-R-C 06
Aravind-R-C 06 - avatar