1

I have an array of images and would like to display them in a div tag using a loop, but none of the images are showing.

const images = ["images/image1", "images/image2", "images/image3"];


            
for (let x = 0; x < images.length; x++) {
                    
    const element = '<img src="' + images[0] + '">'
    let pic = document.querySelector('.images').innerHTML += element;
    pic;
}
3
  • You don't need the pic variable. Commented Feb 14, 2022 at 23:25
  • 2
    images[0] every time you read the first image. You should be using x which is the index of the array. images[x] Now if the images do not show up, then you have an issue with the path to the image. Commented Feb 14, 2022 at 23:27
  • You should move away from string creation / manipulation and instead create actual elements and append them. It is true that the document initially exists as HTML text, but the browser parses it into the DOM, and inserting elements into the DOM is much more convenient (and more performant) than adding together a bunch of strings with various quotes all over the place. Commented Feb 14, 2022 at 23:51

3 Answers 3

1

You are only ever referencing the first index of the array since you used images[0]. You should be using the index.

const images = ["http://placekitten.com/200/300", "http://placekitten.com/100/300", "http://placekitten.com/400/300"];

for (let x = 0; x < images.length; x++) {            
    const element = '<img src="' + images[x] + '">'
    document.querySelector('.images').innerHTML += element;
}
<div class="images"></div>

You you want to use innerHTML to add the images, using map() and join() would be a better solution since you only will update the DOM once.

const images = ["http://placekitten.com/200/300", "http://placekitten.com/100/300", "http://placekitten.com/400/300"];


const myHtml = images.map(function(path) {
  return '<img src="' + path + '">';
}).join('');

document.querySelector('.images').innerHTML = myHtml;
<div class="images"></div>

Other option is creating the elements and appending them.

const images = ["http://placekitten.com/200/300", "http://placekitten.com/100/300", "http://placekitten.com/400/300"];

const outElem = document.querySelector('.images');

images.forEach(function (path) {
  const img = document.createElement("img");
  img.src = path;
  outElem.appendChild(img);
});
<div class="images"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

when I try this, I get an error: Uncaught TypeError: document.querySelector(...) is null
So that means it did not find your element....
0

try this:

const images = ["images/image1", "images/image2", "images/image3"];


            
for (let x = 0; x < images.length; x++) {
                    
    const element = '<img src="' + images[x] + '">'
    document.querySelector('.images').innerHTML += element;
   
}

3 Comments

let element should be const
@epascarello element willl be reassigned that's why I used let
It is defined in a block and value is not set again, it should be const
0

Try appendChild:

const images = ["images/image1", "images/image2", "images/image3"];
const container = document.querySelector('#images');
for (let image of images) {
  const htmlImage = document.createElement("img");
  htmlImage.src = image;
  container.appendChild(htmlImage);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.