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

382
Visualizações
How can I display api data in an html list?

I am working on a website that will be pulling data from an api and displaying the elements in a list. I got this code from another online source, but only the hardcoded data shows, not the data from the api. Is there an issue with my javascript?

Here is my current code below:

<!DOCTYPE html>
<html>
    <head></head>

    <body>
        <ul id="myList"></ul>
        <script>

            let displayList = ['Apples', 'Oranges', 'Grapes', 'Berries', 'Watermelon'];

            fetch(`https://pokeapi.co/api/v2/pokemon`)
            .then(function(response){
                return response.json()
            })
            .then((data) => {
                data.results.forEach((item) => {
                    displayList.push(item.name)
                })
                console.log(displayList)
            })
            .catch((err) => {
                console.log(`Error fetching: ${err}`)
            });

            var list = document.getElementById("myList");
            displayList.forEach((item) => {
                let li = document.createElement("li");
                li.innerText = item;
                list.appendChild(li);
            });

        </script>
    </body>
</html>


I have also tried to put the api pull in a header script tag so it pushes the data before the body is loaded like this:

<!DOCTYPE html>
<html>
    <head>
        <script>
            
            let displayList = ['Apples', 'Oranges', 'Grapes', 'Berries', 'Watermelon'];

            fetch(`https://pokeapi.co/api/v2/pokemon`)
            .then(function(response){
                return response.json()
            })
            .then((data) => {
                data.results.forEach((item) => {
                    displayList.push(item.name)
                })
                console.log(displayList)
            })
            .catch((err) => {
                console.log(`Error fetching: ${err}`)
            });

        </script>
    </head>

    <body>
        <ul id="myList"></ul>
        <script>

            var list = document.getElementById("myList");
            displayList.forEach((item) => {
                let li = document.createElement("li");
                li.innerText = item;
                list.appendChild(li);
            });

        </script>
    </body>
</html>

The data from the pokemon api will be logged in the console, but not in the list that the user will see.

NOTE: This is just a proof of concept, the the code will be moved/formatted as needed after, also I will be my own custom-made api for the final project if this information changes anything

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

0

You need to call the display part again, when you receive your new data. So in .then add the following:

.then((data) => {
    data.results.forEach((item) => {
        displayList.push(item.name)
    })
    displayList.forEach((item) => {
       let li = document.createElement("li");
       li.innerText = item;
       list.appendChild(li);
   })
    console.log(displayList)
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Because you are not using async approach for your displaying part. I just added another then after your api call to show you possible solution

<!DOCTYPE html>
<html>
    <head></head>

    <body>
        <ul id="myList"></ul>
        <script>

            let displayList = ['Apples', 'Oranges', 'Grapes', 'Berries', 'Watermelon'];

            fetch(`https://pokeapi.co/api/v2/pokemon`)
            .then(function(response){
                return response.json()
            })
            .then((data) => {
                var list = document.getElementById("myList");
                data.results.forEach((item) => {
                    displayList.push(item.name);
                    let li = document.createElement("li");
                    li.innerText = item.name;
                    list.appendChild(li);
                })
                console.log(displayList)
            })
            .catch((err) => {
                console.log(`Error fetching: ${err}`)
            });

             

        </script>
    </body>
</html>

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