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

16th Apr 2019, 10:07 AM
José Guilherme
José Guilherme - avatar
1 Resposta
+ 7
You should do: window.onload = function () { // JS code here } The error is because JS is loaded before DOM ( HTML )
16th Apr 2019, 10:38 AM
VEDANG
VEDANG - avatar