document.getElementById doesn't work

Hi, I am a beginner and just started learning Javascript. I’m having problems using document.getElementById, “Test” doesn’t show.

HTML code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
        <meta charset="utf-8">
          <p id="test"></p>
          <script src="test.js"></script>

Javascript code:

 document.getElementById("test").innerText = "Test";

I would be grateful, if anybody can help me!

I tried to reproduce the problem, it seems to work just fine.
Make sure your test.js is in the same directory with your html file,
Try changing the src=“test.js” to src="./test.js" maybe…

I tried both, but it doesn’t seem to work. :confused:

what if you write the javascript code inside the script tag?

Welcome, zah.

It would be easier to help you, if you hosted your content somewhere: is a good place to start.

Also, it is difficult without knowing your workspace - Where your files are (file structure), and what tools you are using to work with HTML and Javascript.

My recommendation is to put your work in a CodePen, and share the link with us, here.

1 Like