+ 1

JS error only in playground

Why i gets this error on sololearn playground while the code works on repl.it? "Cannot set property 'innerHTML' of null" my code html: <!DOCTYPE html> <html> <head> <title>repl.it</title> </head> <body> <p id="demo">hello</p> <script src="script.js"></script> </body> </html> js: var a=document.getElementById("demo"); a.innerHTML="how r u"; code works fine on repl

18th Aug 2020, 4:47 PM
Abdul Majeed Faraz
Abdul Majeed Faraz - avatar
6 ответов
+ 2
ah! my mistake, its window.onload not body
18th Aug 2020, 5:19 PM
Rei
Rei - avatar
+ 2
use load event, maybe js run before the page able to render anything body.onload = function(){ // js here }
18th Aug 2020, 4:52 PM
Rei
Rei - avatar
+ 1
thanks but didnt worked
18th Aug 2020, 4:56 PM
Abdul Majeed Faraz
Abdul Majeed Faraz - avatar
+ 1
ur guess was right i put the function in setTimeout and it worked.
18th Aug 2020, 5:02 PM
Abdul Majeed Faraz
Abdul Majeed Faraz - avatar
+ 1
lol i hate some alphabates of function of js are capitalize :p
18th Aug 2020, 5:03 PM
Abdul Majeed Faraz
Abdul Majeed Faraz - avatar
+ 1
oh lol; thanks though
18th Aug 2020, 5:20 PM
Abdul Majeed Faraz
Abdul Majeed Faraz - avatar