0
Why does the javascript code inside the "JS" window display an error ? Is it possible to work in this window ?
Code : var text = document.createTextNode("Hello World"); var div = document.getElementById("text"); div.appendChild(text); The error refers to the line with the following code: div.appendChild(text); Error : Uncaught TypeError : Cannot read property 'appendChild' of null The code only works in the "HTML" window as below <script type="text/javascript"> var text = document.createTextNode("Hello World"); var div = document.getElementById("text"); div.appendChild(text); </script
1 Answer
+ 7
You should do:
window.onload = function () {
// JS code here
}
The error is because JS is loaded before DOM ( HTML )