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

168
Visualizações
Replace results of API fetch in browser window with results of a new call (JavaScript)

I have three similar functions that call JSON data from an API using HTML buttons, and change it to a string using stringify. The results display in a div, and each new call adds to the current browser page, making it longer. I would like to have each new call replace any previous data displayed in the page.

If I understand correctly, I can't use ReplaceChild once the JSON is stringified. I have tried using string.replace(), but perhaps I am using it incorrectly.

Here is the working code I'm trying to build on:

<body>
<button onclick="awardees()">View NDNP Awardees</button>
<button onclick="ocrFeed()">View Recently-Added OCR'd Pages</button>
<button onclick="batches()">View Available Batches</button>

<script>
function awardees(){
    fetch('https://chroniclingamerica.loc.gov/awardees.json')  
.then(function(response) {
                return response.json();
            })
            .then(function(myJson) {
                var string = JSON.stringify(myJson);
                    document.getElementById('results').innerHTML += "<br/>"+string;
            });
}

function ocrFeed(){
fetch('https://chroniclingamerica.loc.gov/ocr.json')  
.then(function(response) {
                return response.json();
            })
            .then(function(myJson) {
                var string = JSON.stringify(myJson);
                    document.getElementById('results').innerHTML += "<br/>"+string;
            });
}

function batches(){
    fetch('https://chroniclingamerica.loc.gov/batches.json')  
.then(function(response) {
                return response.json();
            })
            .then(function(myJson) {
                var string = JSON.stringify(myJson);
                    document.getElementById('results').innerHTML += "<br/>"+string;
            });
}
</script>

<div id ="ticket">
<span id="results"></span>
</div>

</body>
</html>

Where would I need to add additional lines, and what method do you think would be best?

Thank you so much!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I understand correctly, you should only replace document.getElementById('results').innerHTML += "<br/>"+string; by document.getElementById('results').innerHTML = "<br/>"+string; and you will replace the div content with new string instead of adding it to the existent one.

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