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

118
Vistas
How I can fetch the API correctly

I have created 10 , each display one id in API. I have used querySelectorAll to find all the and use for loop to make the content of each contains one id in API but when I try to code, as you can see, one will contain all ten id and I don't know how to make it possible

function onResponse(response) {
    return response.json();
}

function data(data) {
    //create a function to display the element 
    var htmls = data.map(function(post) {
        return `<li>
                <h2> ${post.id}</h2>
                </li>`
    });

    
    var html = htmls.join('');
    const p = document.querySelectorAll('span');
    for (let i = 0; i < p.length; i++) {
        p[i].innerHTML = html;
    }

}
fetch('https://jsonplaceholder.typicode.com/users').then(onResponse).then(data);
   

<body>
    <form>
        <div>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>

            <span></span>

            <span></span>

            <span></span>
            <span></span>

            <span></span>



           
        </div>
    </form>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

html is already the HTML of each id. You are assigning it to each span's innerHTML, causing there to be 9 duplicates.

Instead, just assign to the span's parent's innerHTML:

function onResponse(response) {
    return response.json();
}

function data(data) {
    //create a function to display the element 
    var htmls = data.map(function(post) {
        return `<li>
                <h2> ${post.id}</h2>
                </li>`
    });

    
    var html = htmls.join('');
    const div = document.querySelector('div');
    div.innerHTML = html;

}
fetch('https://jsonplaceholder.typicode.com/users').then(onResponse).then(data);
<body>
    <form>
        <div>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>

            <span></span>

            <span></span>

            <span></span>
            <span></span>

            <span></span>



           
        </div>
    </form>
</body>

</html>

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