- I have Windows 11.
- I’m new to learning JS and running Live Server.
- I’m running Visual Studio Code v1.119 and the Live Server extension.
- I have made my HTML file and script.js file and the HTML file references the script.js file like this:
<script src="./script.js"></script>They are in the same directory.
When I clicked Go Live at the bottom of the VSC window a Chrome browser window opened up, and it displays my web page. Here’s my Javascript:
console.log("Hello");
//Get element by id
let hdr = document.getElementById('myheader');
console.log(hdr);
hdr.style.color = "red";
The console displays “Hello” just fine. But when I display “hdr” it is null. It is like “document” does not exist at all.
In this line hdr.style.color = "red"; I get an error in the Chrome console: “script.js:6 Uncaught TypeError: Cannot read properties of null (reading ‘style’)”.
The next error in the Chrome console below that is: “Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received”.
I must be doing something wrong but I’m not sure what. Can someone help please?
Thank you.
EDIT: The JS tutorial I’m doing is from Freecodecamp and is here: https://www.youtube.com/watch?v=c6IyCwAV6BY. It is from October 2025 so it’s not that old. The styling portion begins at 26:14.
PROBLEM: I’m trying to get and set the styles for elements already in the HTML file and none of that works due to this error.