0
Why does it not work? - fixed
4 Respostas
+ 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)
+ 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
+ 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
+ 1
Thankyou for your help