Jquery: Incrimentation for each set of elements in more than 1 div
- by Jack
I'm making a Jquery slideshow. It lists thumbnails, that when clicked on, reveal the large image as an overlay. To match up the thumbs with the large images I'm adding attributes to each thumbnail and large image. The attributes contain a number which matches each thumb to its corresponding large image. It works when one slideshow is present on a page. But I want it to work if more than one slideshow is present.
Here's the code for adding attributes to thumbs and large images:
thumbNo = 0;
largeNo = 0;
$(this + '.slideshow_content .thumbs img').each(function() {
thumbNo++;
$(this).attr('thumbimage', thumbNo);
$(this).attr("title", "Enter image gallery");
});
$(this + '.slideshow_content .image_container .holder img').each(function() {
largeNo++;
$(this).addClass('largeImage' + largeNo);
});
This works. To make the incrementation work when there are two slideshows on a page, I thought I could stick this code in an each function...
$('.slideshow').each(function() {
thumbNo = 0;
largeNo = 0;
$(this + '.slideshow_content .thumbs img').each(function() {
thumbNo++;
$(this).attr('thumbimage', thumbNo);
$(this).attr("title", "Enter image gallery");
});
$(this + '.slideshow_content .image_container .holder img').each(function() {
largeNo++;
$(this).addClass('largeImage' + largeNo);
});
});
The problem with this is that the incrimenting operator does not reset for the second slideshow div (.slideshow), so I end up with thumbs in the first .slideshow div being numbered 1,2,3 etc.. and thumbs in the second .slideshow div being numbered 4,5,6 etc. How do I make the numbers in the second .slideshow div reset and start from 1 again?
This is really hard to explain concisely. I hope someone gets the gist.