-1

index.html:13 Uncaught TypeError: Cannot set property 'innerHTML' of null

the code run fine but why am i getting the above error?

i already tried running the script before and after the html element, still getting error.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <script type="text/javascript">
        var txt1 = "some text else"; 
    </script>

    <script type="text/javascript">
        var txt = document.write("some text here");
        document.getElementById('ii').innerHTML = txt; 
    </script>
</head>
<body>

<p id="ii"></p>
<script>
    document.write(txt1);
</script>

</body>
</html>
Ali RJ
  • 149
  • 3
  • 11
  • The element with the id `ii` is not in the DOM at the time when you do `document.getElementById('ii')` – t.niese Apr 30 '16 at 07:44

1 Answers1

1

Your javascript is executed before the DOM has been loaded. Put all <script>-Tags just before the closing </body> tag.

marvinhagemeister
  • 2,801
  • 1
  • 22
  • 27
  • About `Your javascript is executed before the DOM has been loaded.`, if you place the ` – t.niese Apr 30 '16 at 07:48