Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda