I bet that you are not running this in an index.html file in the browser, so this is not VS Code problem, you are probably running this in a node console or something, there is no html or document in what you are trying to test and that is why you are getting this error.
Make sure your html file is referencing your javascript file.
<!DOCTYPE html>
<html>
<body>
<h1>This is my HTML file</h1>
<script src="script.js"></script>
</body>
</html>
The script element should be in your html with the name of your js file
<script src="script.js"></script>
Solution 3 :
This not the vscode issue please check you are running the right file
Problem :
why is document.getElementById() not working in VS Code?? I keep getting this error: “Uncaught ReferenceError ReferenceError: document is not defined”. I’m new to VS Code but I’m assuming the reason It’s not working is that I need to install some extension to make it work. The same code is working on Replit but not VS code. I installed JS(ES6) Snippets, Open-in browser, Live Preview and Live Server. It’s very simple 2-line code just to experiment but it’s not working. It’s driving me crazy!
let head = document.getElementById('change')
head.innerText = 'hello'
Comments
Comment posted by Chris G
you are gonna get more errors if the text you want to apply to your innerText includes quotes like the word
Comment posted by Lemondoge
I don’t think this is exactly the problem you’re trying to solve, but the apostrophe of the
Comment posted by David
(1) The code shown has a syntax error and won’t “work”
Comment posted by Decent Explanation about Excaping
When your color scheme is off, you know something is screwed up. It’s the
Comment posted by Nir
I’m sorry! I just wrote ‘why isn’t this working?’ for the purpose of this question. My actual code just has the word ‘hello’ set to innerText
Comment posted by mruanova
i edited my answer, you had a problem with string concatenation
Comment posted by Nir
omg i feel so stupid ♂️! I completely forgot to link js to html. Sorry everyone
Comment posted by oalva-rez
@Nir No need to feel stupid my guy! These are the mistakes that help you become a better developer along with a better problem solver.