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

182
Visualizações
Using string from .js file in HTML

Hello everyone, below I have script.js and index.html. I want to use the variable outputHTML from script.js in the script tags of index.html so that I can print them in table form on my page (I'm pretty sure I have to do it this way because I'm using node). However index.html won't seem to recognize the vale of outputHTML from script.js and I have no idea why. Hopefully someone can help, thanks in advance.

script.js

var outputHTML = '';     
var animals = ["cat", "dog", "goat", "turkey", "buffalo"];

// Loop over our arrays and create our html string
outputHTML += "<table>";
for (var i = 0; i < animals.length; i++) {
    outputHTML += "<tr>";
    outputHTML += "<td>" + animals[i] + "</td>";
    outputHTML += "</tr>";
}
outputHTML += "</table>";

export { outputHTML };

------------------------------------------------=

//index.html file

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript"
        src="script.js">
    </script>
    
</head>

<body>
    <button onclick= "f()">
        Click To Access Animals
    </button>

    <div>
        <p id="text" style="color:purple;
            font-weight:bold;font-size:20px;">
        </p>
    </div>
    
    <script type="text/javascript">
        import { outputHTML } from './script.js';

        function f() {
            
           document.getElementById("text").innerHTML = outputHTML;

        }

    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
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