Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

398
Views
How to pass data from Node.js to html

I have a simple program and some data strings that I want to pass to HTML page that hosted locally, but I'm getting error: ReferenceError: document is not defined

There's my .js file index.js:

let some_string = "some data";

document.getElementById("dataButton").onclick = function(){
    var paragraph = document.createElement("p");
    var text = document.createTextNode(some_string); 
    paragraph.appendChild(text);
    var element = document.getElementsByTagName("body")[0];
    element.appendChild(paragraph);
}

And there's my HTML test.html:

<!DOCTYPE html>
<html>

<head>
    <title>New title</title>
</head>

<body>
    <h1>Data page</h1>

    <button id="dataButton">Add data</button>
    <script src="index.js"></script>
</body>

</html>

Many thanks if someone come up with idea what am I doing wrong!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Update your files to the following

let some_string = "some data";
const dataButton = document.getElementById("dataButton");

dataButton.addEventListener("click", () => {
  var paragraph = document.createElement("p");
  var text = document.createTextNode(some_string);
  paragraph.appendChild(text);
  var element = document.getElementsByTagName("body")[0];
  element.appendChild(paragraph);
});
<!DOCTYPE html>
<html>
  <head>
    <title>New title</title>
  </head>

  <body>
    <h1>Data page</h1>
    <button id="dataButton">Add data</button>
    <script src="./index.js"></script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!