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

321
Visualizações
Looping through an array and injecting all the values into <select><option>

So basically I have this dogs API (https://docs.thedogapi.com/) and im trying to loop somehow and show every single dog's name from the API server into my tag so people can choose the dog name and show photos and info about it

<label for="dogs" name="dogsInput" id="dogsLabel">
    <select id="dogTypes">
        <option value=""></option>
    </select>



// JavaScript

document.querySelector('button').addEventListener('click', getFetch)

function getFetch(){
    const api_key = '' // not sure if I should post it here so I hide it
    const input = document.querySelector('select').value
    const url = `https://api.thecatapi.com/v1/breeds/search`
    fetch(url)
        .then(res => res.json())
        .then(data => {
            console.log(data[0])
            document.querySelector('#name').innerHTML = data[0].breeds[0].name
            document.querySelector('img').src = data[0].url
            document.querySelector('h3').innerHTML = "Temperament: " + data[0].breeds[0].temperament
            document.querySelector('h4').innerHTML = "Life span: " + data[0].breeds[0].life_span
        })
        .catch(err => {
            console.log(`error ${err}`)
        })
         
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'll just assume that the API is returning data, and I'll also assume the data structure of JSON object.

Your fetch should look something like this:

...
fetch(url)
    .then(res => res.json())
    .then(data => {

        if (!data) return; // Do something here

        // Get the select element
        var select = document.getElementById("dogTypes");

        // Iterate the data, create a <option> element and append it to
        // the select
        for (let i = 0; i < data.length; i++) {
            const element = data[i];
            
            var option = document.createElement("option");
            option.text = element.Name;
            option.value = element.Value;

            select.appendChild(option);

        }

    })
    .catch(err => {
        console.log(`error ${err}`)
    })
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