New course! Every coder should learn Generative AI!
Try a free lesson0
Can anyone explain me,why I am getting error in my JavaScript ?
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
}
0
🍇 Alex Tușinean 💜 what do u mean by "code that should load after html" ?
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
0
🍇 Alex Tușinean 💜 oo yes,I had done such a dumb mistake🥲,Thank you so much🙂