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

243
Visualizações
How can I fetch data to display on a new HTML page after clicking a button on a main index.html page?

I have two html files (index.html and allUsers.html) and a JavaScript index.js The index.html has a button allUsersButton.

My goal is that when the allUsersButton is clicked, I should see the allUsers.html page, and be able to see the fetched data which should be injected into the div named allUsersDivId.

So far, no data loads on the allUsers.html page and I get an error on the console "Uncaught TypeError: Cannot set properties of null (setting 'onclick')".
Should both the index.html and the allUsers.html have the script linked in them? What's the best way to put this together?

index.html

<body>
    <form action="http://localhost:7050/hello" method="POST">
        <label for="username">User name:</label>
        <input type="text" id="username" name="username"><br><br>      
        <input type="submit" value="Submit">
    </form>

    <button type="button" id="allUsersButton">All Users</button>
    
    <script src="index.js"></script>
</body>

allUsers.html

<body>
    <div id = "allUsersDivId">
    </div>
    <script src="index.js"></script>
</body>

index.js
Here I have a function for fetching and inserting the data into the div allUsersDivId which is in the allUsers.html, and an onClick listening on the allUsersButton which is in the index.html.

document.getElementById("allUsersButton").onclick = function() {displayAllUsers()};

function displayAllUsers() {
    window.location.href='allUsers.html'
    fetch("http://localhost:7050/allusers")
    .then((response) => {
        if (response.ok) {
        return response.json();
        } else {
        throw new Error("NETWORK RESPONSE ERROR");
        }
    })
    .then(data => {
        for(var i = 0; i < data.length; i++){
           const userName = data[i].username 
           const userNameDiv = document.getElementById("allUsersDivId")
        
           const heading = document.createElement("h1")
           heading.innerHTML = userName
           userNameDiv.appendChild(heading)
        }
    })
.catch((error) => console.error("FETCH ERROR:", error));    
}

What's the best way to link this all together?

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