1

I am trying to create webpage where the user clicks a button and then the page displays images saved in an array.When I run the code, It displays the source of the images, but not the images. Any tips on what I am doing wrong? Thanks in advance

<!DOCTYPE html>
<html>
<body>

<h1>Slideshow</h1>

<p>Holiday Slideshow</p>


<button type="button" onclick="beginShow()">View Slideshow</button>

<p id="p1"><img id="pic1" src="./assets/pic1.jpg">
Click on "View Slideshow". Click to display slideshow</p>
<script>
var list = [
    "/assets/pic1.jpg",
    "/assets/pic2.jpg",
    "./assets/pic3.jpg",
    "./assets/pic4.jpg"
];

var index = 0;

function beginShow() {
    setTimeout(myTimeout1, 2000) 
    setTimeout(myTimeout2, 4000) 
    setTimeout(myTimeout3, 6000)
    setTimeout(myTimeout4, 8000) 
}
function myTimeout1() {
    document.getElementById("p1").innerHTML = list[0];
}
function myTimeout2() {
    document.getElementById("p1").innerHTML = list[1];
}
function myTimeout3() {
    document.getElementById("p1").innerHTML = list[2];
}
function myTimeout4() {
    document.getElementById("p1").innerHTML = list[3];
}
</script>

    </body>
</html>
4
  • you should change the src of pic1 Commented Apr 6, 2016 at 13:23
  • document.getElementById("pic1").src=list[x] Commented Apr 6, 2016 at 13:26
  • Thanks for your reply, when I change the code to what you've suggested, (replacing x with 0,1,2,3) the images do not load when viewing in browser. Commented Apr 6, 2016 at 13:45
  • can you make a fiddle ? so we can test it quickly Commented Apr 7, 2016 at 2:05

2 Answers 2

1

You need to set those urls to the image instead of its container.

Your code should be written like this.

var list = [
    "/assets/pic1.jpg",
    "/assets/pic2.jpg",
    "./assets/pic3.jpg",
    "./assets/pic4.jpg"
];
function showImage(i){
    i = i || 0;
    setTimeout(function(){
        document.getElementById("pic1").src = list[i];
        i < list.length - 1 && showImage(++i);
    },2000);
}
showImage();
Sign up to request clarification or add additional context in comments.

7 Comments

You passed in src as the function variable but then used i without initializing it.
@AlexanderDixon Thanks for the hint.
The iteration is not working properly. In the console, i is returning NaN. Essentially, the #pic1 src attribute is always returning the initial value, or the first picture. It does not increment.
@AlexanderDixon I've fixed it.
It returned undefined a few times. This is a learning experience for me, not so much a critique on your code. Could you please tell if the only way an undefined variable will work is in a for loop method?
|
0

If you are going to do it this way you have to change what you assign to innerHTML:

document.getElementById("p1").innerHTML = '<img src=' + list[0] + '>;

But I recommend you rather change src attribute of image to list[0]:

document.getElementById('pic1').setAttribute('src', list[0]);

5 Comments

Thanks for your reply. When I change it to the .setAttribute option, pictures 2,3 and 4 do not load when viewing the code through a web browser. Any suggestions?
Are you sure you didn't just copy pasted it? You need to change parameter too (so change list[0] to list[1], list[2] etc.)
I changed the parameter for all four lines (so it runs 0 to 3)
Do you get any error in network tab of dev tools? Because this way you are changing src attribute so it should be downloaded (or at least tried) by the browser...
I needed full stops before the source in the array. Thanks for your help!

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.