Skip to content Skip to sidebar Skip to footer

How Can I Change An Image Using Javascript (jquery & Iviewer) By Adding 1 To The Img Src Incrementally?

Checked various questions on SO, but found nothing that quite matches what I'm after... I'm buildling an image viwer using a combination of jQuery and iViewer. Here's the code that

Solution 1:

This should do the trick...

var i = 0;

$("#chimg").click(function()
{
    i++;
    iv1.iviewer('loadImage', "img/" + ("0" + i).slice(-2) + ".jpg");
    returnfalse;
});

This...

("0" + i).slice(-2)

...adds 0 to the beginning of whatever value is stored in i and then takes the last 2 characters, so you've got your 0 padded number.

Solution 2:

i have never used iViewer before but first option off the top of my head is can you get the 'src' attribute for the img? if you have the source you can get the number via regular expression

var src = $('#chimg').attr('src');
var m = parseInt(src.match(/\d+/g));
n = m+1;
src = src.replace(m, n);
iv1.iviewer('loadImage', src);

then whatever else you have to do.

see it work

Post a Comment for "How Can I Change An Image Using Javascript (jquery & Iviewer) By Adding 1 To The Img Src Incrementally?"