Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

403
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda