1

I am trying to display an array of images using JavaScript, but he doesn't seem to find the images. I do get however this messages on screen: [Object HTMLImage Element]

Any help would be appreciated. I'm a student trying to learn.

let slideshow;
let currentItem = 0;
let items = [6];
//let ready = true;
items[0] = new Image();
items[0].src = "../images/bier%20tafe.jpeg";

items[1] = new Image();
items[1].src = '../images/pintjes.jpeg';

items[2] = new Image();
items[2].src = '../images/flesjes.jpeg';

items[3] = new Image();
items[3].src = '../images/grain.jpg';

items[4] = new Image();
items[4].src = '../images/cigar.jpg';

items[5] = new Image();
items[5].src = '../images/whiskey.jpg';

document.addEventListener('DOMContentLoaded', function(ev){
  slideshow = document.querySelector('.slideshow');
  slideshow.addEventListener('click', next);
  //call the event once to start the show
  slideshow.dispatchEvent(new MouseEvent('click'));
  //next(); //the line above is the same as calling next()
});

function next(ev){
  //this runs when the slideshow has been clicked
  ev.preventDefault();
  //call the function to remove the oldest item (if it exists)
  removeItem();
  //add the new one
  let item = document.createElement('div');
  item.textContent = items[currentItem];
  item.classList.add("slideshow-item");
  slideshow.appendChild(item);
  setTimeout(function(){
    item.classList.add('active');
    //this could fail if the user clicked twice within 20 milliseconds
  }, 20);
  currentItem++;
  if(currentItem > items.length-1){
    currentItem = 0;
  }
}

2
  • 2
    Just an FYI - let items = [6]; is not making a new array with the size of 6, but creating a new array and setting 6 at index 0 Commented May 23, 2018 at 19:57
  • Maybe this: item.textContent = items[currentItem]; You should be probably creating elements of type img, not div. Commented May 23, 2018 at 20:45

1 Answer 1

1

It looks to me your mistake is in using:

item.textContent = items[currentItem];

This is trying to set a text element, and since you're giving it an image object, it's just giving you the textContent value of that object "[Object HTMLImage Element]".

Instead you need to actually place the image inside your div:

item.appendChild( items[currentItem] );

This will append the image as a child into the div you specified.

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

1 Comment

Glad I could help. If you feel like the answer is adequate, please accept it as the answer to help anyone who finds your question in the future.

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.