basic javascript image gallery logic error?

user3720610

i've attempted to make a very simple image gallery, which is functional for the most except for the back function. the next function loops around perfectly, but the back function includes a blank image (where it cant locate one), i think theres something wrong with my logic but im not sure where, if someone could point it out id be very grateful. the error message is :GET file:///G:/eject/undefined net::ERR_FILE_NOT_FOUND

 var myImage= new Array(); 
myImage[1]="images/product/linkin.jpg";       
myImage[2]="images/product/lisagerrard.jpg";
myImage[3]="images/product/maddy.jpg";
myImage[4]="images/product/mariah.jpg";

var image = 1;
function next()
{ 
    image++;
    document.getElementById("picture3").style.background = 'url(' + myImage[image] + ')';
    if (image==4)
    {
    image = 0;
    }
  }

function  back()
{
 image--;
    document.getElementById("picture3").style.background = 'url(' + myImage[image] + ')';
    if (image== 0)
    {
    image = 5;
    }
}

html :

  <a id ="f1" onclick="back();return false;" href="#"> back </a> <a id ="f2" onclick="next();return false;" href="#"> next </a>
MrCode

The issue is in the back function, you decrement the counter, then try to display the image before you do the out of bounds check. To resolve, move the out of bounds check to before you try to display the image:

function  back()
{
    image--;
    if (image== 0)
    {
        image = 4; // also go to 4 not 5
    }
    document.getElementById("picture3").style.background = 'url(' + myImage[image] + ')';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery basic slider into image gallery with thumbnails

From Dev

JavaScript code for my Image Gallery

From Dev

Javascript splice for filtering image gallery

From Dev

JavaScript and Bootstrap image gallery with different image sizes

From Dev

logic error in javascript code

From Dev

Error while loading image from gallery in fragment

From Dev

getClientOriginalName() error while trying to upload image gallery

From Dev

Pure javascript image gallery with fwd and bck function

From Dev

How to filter image gallery by tags using Javascript?

From Dev

How to create a mini image gallery using javascript?

From Dev

Fading images in, with the current basic JQuery Image gallery i have used

From Dev

Basic Image Viewer with javaScript

From Dev

Android choose image from gallery showing memory error

From Dev

Looking for a method to make this HTML/CSS image gallery transition with javascript

From Dev

javascript gallery not displaying main image when thumbnails clicked

From Dev

Outputting imgur image URLs from a gallery using the API using javascript

From Dev

How do I trigger a lightbox gallery in html or javascript for a single image?

From Dev

Masonry Image gallery not working, inline Javascript not taking any effect

From Java

Question about JavaScript Math.random() and basic logic

From Dev

Custom Image Gallery for IOS?

From Dev

Trying to make an image gallery

From Dev

Responsive image gallery solution

From Dev

Captured image not appearing in gallery

From Dev

How to open an image in the gallery?

From Dev

Image gallery / float issue

From Dev

Gallery image does not change

From Dev

Saving image in Gallery

From Dev

Hide image from gallery

From Dev

Custom Gallery Image Picker

Related Related

  1. 1

    jQuery basic slider into image gallery with thumbnails

  2. 2

    JavaScript code for my Image Gallery

  3. 3

    Javascript splice for filtering image gallery

  4. 4

    JavaScript and Bootstrap image gallery with different image sizes

  5. 5

    logic error in javascript code

  6. 6

    Error while loading image from gallery in fragment

  7. 7

    getClientOriginalName() error while trying to upload image gallery

  8. 8

    Pure javascript image gallery with fwd and bck function

  9. 9

    How to filter image gallery by tags using Javascript?

  10. 10

    How to create a mini image gallery using javascript?

  11. 11

    Fading images in, with the current basic JQuery Image gallery i have used

  12. 12

    Basic Image Viewer with javaScript

  13. 13

    Android choose image from gallery showing memory error

  14. 14

    Looking for a method to make this HTML/CSS image gallery transition with javascript

  15. 15

    javascript gallery not displaying main image when thumbnails clicked

  16. 16

    Outputting imgur image URLs from a gallery using the API using javascript

  17. 17

    How do I trigger a lightbox gallery in html or javascript for a single image?

  18. 18

    Masonry Image gallery not working, inline Javascript not taking any effect

  19. 19

    Question about JavaScript Math.random() and basic logic

  20. 20

    Custom Image Gallery for IOS?

  21. 21

    Trying to make an image gallery

  22. 22

    Responsive image gallery solution

  23. 23

    Captured image not appearing in gallery

  24. 24

    How to open an image in the gallery?

  25. 25

    Image gallery / float issue

  26. 26

    Gallery image does not change

  27. 27

    Saving image in Gallery

  28. 28

    Hide image from gallery

  29. 29

    Custom Gallery Image Picker

HotTag

Archive