Why does it not work? - fixed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
19th Dec 2019, 7:15 AM
GrayingOut
GrayingOut - avatar
4 Answers
+ 2
window.onload = function() { document.getElementById("demo").innerHTML = "Hi"; } The element isn't rendered yet by the browser, getElementById failed in fetching the desired element due to this. Solution Wrap the code in the 'load' event handler. This ensures that the element is available for you to refer because the page rendering should've been completed when the 'load' event is fired. Remote images, external scripts, stylesheets would have been readily available when the 'load' event fires. (Edited)
19th Dec 2019, 7:34 AM
Ipang
+ 4
I'm stumped - I think we're missing something obvious but I can't find the error - even if I assign innerHTML to other elements it throws the same error
19th Dec 2019, 7:27 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 3
ok mate I tested it out - for some reason whatever you put in JS file, it gets a null reference. Try adding this just before the ending body tag <script> document.getElementById("demo").innerHTML = "Hi"; </script> No idea if I'm being dumb right now
19th Dec 2019, 7:38 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 1
Thankyou for your help
19th Dec 2019, 7:41 AM
GrayingOut
GrayingOut - avatar