0

As an absolute newbie to Javascript I think my issue lies with the code. I created an html index file and a home.js file and get a type error. The error is not obvious to me, perhaps someone has an answer.


index file


<body>
    <script src="home.js">
        <p id="SomeText"></p>

    </script>
</body>
</html>

home.js file


console.log('good morning');

// alert('Oh happy day')

document.getElementById("SomeText").innerHTML = 'Okay';
Bee Bloggs
  • 11
  • 4
  • The script runs before the element exists on the page. That's why `addEventListener("load")` or `addEventListener("DOMContentLoaded")` exists. Also, you added the HTML code inside your ` – Jorge Fuentes González Jan 04 '20 at 15:08
  • As the linked question says, getting `null` back from `getElementById` means the elemtn wasn't found. In this specific case, the element wasn't found because it doesn't exist on the page -- you've put the markup for it **within** the `script` tag. In a `script` tag with a `src` and content (such as yours), the content is considered "documentation" for the script and ignored by the browser. You never put HTML within the `script` tag, put it **before** the `script` tag so that the script can find it. – T.J. Crowder Jan 04 '20 at 15:10
  • @JorgeFuentesGonzález, not sure what you mean. Where do you see html within the script? The html comes after the script and body tag in the code I submitted. – Bee Bloggs Jan 04 '20 at 15:47
  • The html is within the open/close script tags. This is not good: https://i.imgur.com/8xydd1v.png – Jorge Fuentes González Jan 04 '20 at 19:10
  • @JorgeFuentesGonzález, now I understand. Thank you!!! – Bee Bloggs Jan 04 '20 at 20:41

0 Answers0