Can anyone explain me,why I am getting error in my JavaScript ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain me,why I am getting error in my JavaScript ?

https://code.sololearn.com/WC1PheY79x46/?ref=app

21st Jul 2022, 12:53 PM
Alok
Alok - avatar
4 Answers
+ 2
The javascript is loaded before HTML, which means you cannot just write let bg = document.getElementById("bg"). If you use a console.log(bg) inside your function, you will see that bg = null. Search on the internet how to use window.onload=function(){ //your code that should load after html }
21st Jul 2022, 1:42 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
0
🍇 Alex Tușinean 💜 what do u mean by "code that should load after html" ?
21st Jul 2022, 4:39 PM
Alok
Alok - avatar
0
You can not acces any html element with js before it loads. The javascript loads first, so if you try to select a html element, like a paragraph, it will fail, because it won t find any html p
21st Jul 2022, 4:47 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
0
🍇 Alex Tușinean 💜 oo yes,I had done such a dumb mistake🥲,Thank you so much🙂
22nd Jul 2022, 1:58 AM
Alok
Alok - avatar