UPDATE: The markup shown in your codepaste is invalid because you have duplicate id attributes in your container divs. You didn't make it clear in your original question that all of the code shown was repeated several times. Change each id="product"
to class="product"
and the following code will work:
jQuery(document).ready(function() {
jQuery('.mainImg').each(function() {
$(this).data("original", this.src);
});
jQuery('.additionalImg a img').hover(function () {
jQuery(this).closest('.product').find('.mainImg').attr("src", this.src);
},function() {
var $main = jQuery(this).closest('.product').find('.mainImg');
$main.attr("src", $main.data("original"));
});
});
Demo: http://jsfiddle.net/s9Rhx/2/
Include the above JavaScript once on your page, not once per product div.
The above works by firstly looping through all of the mainImg
divs and storing their original default image src. Then it binds the event handler directly to the img elements in the thumbnail anchors. On hover it uses .closest()
to navigate up to the containing class="product"
div (don't forget to change the id
to a class
), then .find()
to get back down to the mainImg
div within the current container.