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

83
Visualizações
I am trying to get my image to display from this for loop from the array object

I am trying to get the links in the array objects to display when the loop goes through but they appear as broken links. broken link code

<p id= "tracks"> </p>
<div id = "images"> </div>
<script>

let songs = [
  {
    title: "Getting Away with It (All Messed Up)",
    artist: "James",
    album: "Pleased to Meet You",
    year: 2001,
    art: "https://upload.wikimedia.org/wikipedia/en/2/2a/JamesPleasedToMeetYou.jpg"
  },
  {
    title: "Renaissance Affair",
    artist: "Hooverphonic",
    album: "Blue Wonder Power Milk",
    year: 1998,
    art: "https://upload.wikimedia.org/wikipedia/en/1/17/Hooverphonic-Blue_Wonder_Power_Milk.jpg"
  },
  {
    title: "White Nights",
    artist: "Oh Land",
    album: "Oh Land",
    year: 2011,
    art: "https://upload.wikimedia.org/wikipedia/en/6/68/Oh_Land_%28album%29.png"
  }
]
  // Javascript Code here
 element_p = document.getElementById("tracks")
 //alert (element_p.innerHTML); 
 let text = "";

 for (let data of songs) {
  let new_paragraph= document.createElement ('p');
 new_paragraph.innerHTML = 'Title: '+ data.title + " <br> Album: "+ data.album+ ' <br> Artist: ' + data.artist + "<br> Year: "+ data.year
  element_p.appendChild(new_paragraph)
 }
element_div = document.getElementById ("images")
let image = "";

for (let photo of songs){
 let img_node = document.createElement ('IMG');
 img_node.setAttribute('src', "data.art");
 document.getElementById("images").appendChild (img_node);
} 

What am I doing wrong? I tried two different ways and this way is the only way at least the broken links show up. Any help or insight is appreciated.

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

0

Instead of 'data.art' you have to pass in the url from each photo you are passing in by using photo.art

about 4 years ago · Juan Pablo Isaza Relatório

0

You should use img_node.setAttribute('src', photo.art). like below example:

...
for (let photo of songs){
  let img_node = document.createElement ('img');
  img_node.setAttribute('src', photo.art);
  document.getElementById("images").appendChild(img_node);
} 
about 4 years ago · Juan Pablo Isaza Relatório

0

You have data.art in quotes so its treated as a string instead of an object. Also, your for loop is declaring photo so you should change data.art to photo.art. Like this:

for (let photo of songs){
  let img_node = document.createElement ('IMG');
  img_node.setAttribute('src', photo.art);
  document.getElementById("images").appendChild (img_node);
} 
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