Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Thumb Photo Another Div Onclick

Hi there, hope for some help, i have a list of thumbnails that each contains a image, but i wish when i click in the thumbnail it appears the image in another div where show the image bigger. But for some reason is not working, here is my code.

Hope for some help

Main Image Big:

<div id="photoHolder" class="centerbar-left">
 <a href="products/13/0.54608100.jpg" class="fancybox" data-fancybox-group="gallery"><img class="item_image" src="products/13/0.54608100.jpg" height="330" width="330"></a> 
<div class="arrow_box"><img src="images/top_image.png" alt=""></div>
  </div>

List Thumbs:

<ul id="sideWall" style="padding-left: 0px;">
<li>
<a href=""><img src="products/13/0.54608100.jpg" width="52px"></a>
</li>
<li>
<a href=""><img src="products/13/0.77981300.jpg" width="52px"></a>
</li>
</ul>

Code Jquery:

$('#sideWall img').click(function()){

var c = $(this.attr("src").replace();

$("#photoHolder").html('<img width="330" height="330" src="'+ c + '" />');

)}

3 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Mauro Bonucci

If I understand you correctly, it's the same image file for both thumbnail and larger image. You just set the width to a smaller value for the thumbnail.

I see a typo here: var c = $(this.attr("src").replace();

You're missing a closing ) after $(this

Also you don't need the .replace() method. This should work:

var c = $(this).attr("src");
Andrew Shook
Andrew Shook
31,709 Points

Mauro, are you just wanting to replace the image or are you wanting to also replace the link href the image is wrapped in?

Thanks a lot Dave, and thank you to Andrew for taking the time to help in this issue.

Best