1

So basically I have a gallery script in jQuery. When a user clicks on a thumbnail image the image attribute src is then transferred to the main gallery img element. However I now want the user to be able to click a next/prev button and iterate through all the images in the folder. I named them all img0.jpg, img1.jpg and so on. I was thinking I could simply get the current img src via "attr()" and "slice()" then increment the number in the img'0', img'1' etc... with a variable. Heres my code. I hope you guys can understand!

$("#next").click(function () {
   var i = 0;
   var sliceImg = $("#gallerycover").attr("src").slice('0', String.length - 5);
   $("#gallerycover").attr("src", sliceImg + i++ +."jpg");
});
<div id="gallerycontainer">
  <img id="gallery" src="" />
</div>

<div id="thumbcontainer">
  <div id="box4"><img class="thumb" src="" /></div>
</div>
0

3 Answers 3

1

Try replacing your following code:

var sliceImg = $("#gallerycover").attr("src").slice('0', String.length - 5);
$("#gallerycover").attr("src", sliceImg + i++ +."jpg");

for this one:

var src = $("#gallerycover").attr("src");
var idx = src.indexOf('img');
var sliceImg = src.slice(idx+3, src.length - 4);
$("#gallerycover").attr("src", (parseInt(sliceImg, 10) + 1) + ".jpg");

See working demo

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

6 Comments

the directory for each image folder set is fairly large e.g. "images/gallery/environments/gameprops/img0.jpg" Will this affect the code you game me?
Based on that path pattern, the code should work, but the best to know is try it out! :-)
can you explain also the parseInt line?? Yes sure I will upvote if it works!
parseInt() is a function to convert from string to number, we need this as a prior step to later increment it by one. The + operator when applied on a number variable acts as a sum, but when applied on a string variable acts as concatenation operator, so we need to convert sliceImg to number with parseInt() so the + acts as a normal sum operator. We also use its second parameter to specify the base we want to convert to, as we want a normal decimal number, so we use 10 as second parameter.
The second parameter to parseInt() is optional but recommended to use it, to avoid errors such as in this example call: parseInt('043') will convert to octal (instead of decimal) because of the leading zero, so if we use parseInt('043', 10) then we are sure it converts to decimal.
|
0

you can use a regular expression to split up your image source string, or you can have your slider keep better track of individual slides by adding an attribute to the thumbnail that your next/previous functions can check and increment without having to parse strings (which can possibly change in the future).

Comments

0
var myInt = 0;
var myInt++;

var sliceImg = $("#gallerycover").attr("src").replace(/\d+/, myInt);

$("#gallerycover").attr("src", sliceImg);

Comments

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.