0

OK guys. I want a larger displayed image to switch when a user clicks on a smaller thumbnail image.

This is my jquery script but so far it doesn't switch. Nothing happens when the thumbnail is clicked.

Its strange because I have two arrow images that slide the thumbsnails across when clicked but for some reason not when the acutal thumb is clicked. What on earth is going on?

JQuery:

$(document).ready(function () {

    // when image with ID thumb is clicked change src 
    $("img#thumb").click(function () {              
        var imgpath = $(this).attr("src");
        $("img#cover").attr("src", imgpath);
    });

    // slide more thumbnails 
    $("#arrowright").click(function () {
        $("#innerthumb").animate({ marginLeft: "-850px" });
    });

    $("#arrowleft").click(function () {
        $("#innerthumb").animate({ marginLeft: "0px" });
    });

    // image change on click
    $("img#thumb").click(function () {
        var newimg = $(this).attr("src");
        $("img#cover").attr("src", newimg);
    });

HTML

                    <div id="mainimage">
                        <div id="inner">
                            <img id="cover" src="images/environments/img0.jpg" width="980px" />
                            <img id="cover-old" src="" width="980px"/>
                        </div>
                    </div> <!--end of mainimage div-->

               <div id="arrowleft"><img src="images/arrowleft.png"></div>
                   <div id="innerthumb">
               <div id="thumb1" ><img id="thumb" src="images/environments/img0.jpg" width="160px" height="80px"/></div>
               <div id="thumb2" ><img id="thumb" src="images/environments/img1.jpg" width="160px" height="80px" /></div>
               <div id="thumb3" ><img id="thumb" src="images/environments/img2.jpg" width="160px" height="80px" /></div>
                   <div id="innerthumb"></div>
               <div id="arrowright"><img src="images/arrowright.png"></div>
brenjt
  • 15,997
  • 13
  • 77
  • 118
johnnyzoo
  • 27
  • 8

1 Answers1

0

You should not have img with same id within the same page. This causes problem with jquery selector.

To solve your problem, you have to change the img id to say t1, t2, t3 and set a class=thumb and then on the jquery part do something like this

$("img.thumb").click(function () {
    var id = $(this).attr("id");
    var newimg = $("#"+id).attr("src");
    $("img#cover").attr("src", newimg);
});
zdesam
  • 2,936
  • 3
  • 25
  • 32