How could I display the first image in array?

My array is just not displaying anything
JS

var imgArray = new Array();

imgArray[0] = new Image();
imgArray[0].src = "https://cdn.mos.cms.futurecdn.net/54LNwFS62fB3XLm2gikEr6-1200-80.jpg";

imgArray[1] = new Image();
imgArray[1].src = "https://lithub.com/wp-content/uploads/2018/06/milky-way.jpg";

function nextImage() {
    var createImg = document.createElement("img");
    createImg.src = imgArray[0];
    document.getElementsByTagName('body')[0].appendChild(createImg);
}

I put comments in a pen I made based on your code. You can find the pen here.

1.) In your code, you tried to define the createImg.src as the first Image object (technically it’s an HTMLImageElement) in the array instead of that Image object’s src. You can read more about the Image element here.

2.) Don’t forget to call the function that you defined.

I hope this helps you!

1 Like

thanks alot was very handy