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

135
Visualizações
I am trying to create a specific html structure using javasacript dyanamically, but don't know how to do it

I fetching some data with api and want o use them and show them on my HTML page using pure javascript. My current HTML:

<button onclick="load()">Load data</button>
<div id="main"></div>

My expected structure to use API data:

<div id="main">
 <div class="wrapper">
   <img src="link will come from API" class="img-class">
   <h6 class="title"></h6>
 </div>
<div class="wrapper">
   <img src="link will come from API" class="img-class">
   <h6 class="title"></h6>
 </div>
<div class="wrapper">
   <img src="link will come from API" class="img-class">
   <h6 class="title"></h6>
 </div>
<div class="wrapper">
   <img src="link will come from API" class="img-class">
   <h6 class="title"></h6>
 </div>
.
.
.
.
.
....
</div>

Here is my js:

function load(){
  fetch("https://jsonplaceholder.typicode.com/photos")
    .then(function(response){
    return response.json()
  })
    .then((response)=>{
    var counter=0;
    for (var data of response) 
    {
      if(counter == 99){
        break;
        alert("end");
      }else{
        var wrapper = document.createElement('div');
        var img = document.createElement('img');
        img.src = data.url;
        img.setAttribute("class","img-class");
        document.getElementById('main').appendChild(img);
        document.getElementById('main').appendChild(img);
        counter++; 
      }
    }
  })
}

I was able to load images but now I want to make a proper structure. Here is working copen link.

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

0

You can create each element, set the attributes, then append it to it's parent. Since you didn't specify the structure of the data your API returns, you'll have to change the code to support your API. In your case, something like this should work.

for (var data of response) {
    const imageUrl = data['image'] // you'd want to change this

    const wrapperDiv = document.createElement('div');
    wrapperDiv.classList.add('wrapper');
    
    const imgElem = document.createElement('img');
    imgElem.src = imageUrl;
    imgElem.classList.add('img-class');
    wrapperDiv.appendChild(imgElem);

    // You didn't specify what you wanted 
    // to do with the title so make sure to add
    // to this part.
    const titleElem = document.createElement('h6');
    titleElem.classList.add('title');
    wrapperDiv.appendChild(titleElem);

    document.getElementById('main').appendChild(wrapperDiv);

}
about 4 years ago · Juan Pablo Isaza Relatório

0

just append img to wrapper

wrapper.appendChild(img)

then append wrapper to the document

document.getElementById('main').appendChild(wrapper)
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