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

General Discussion

Creating a Simple Lightbox Last Challenge

I am confused about what this challenge is asking and what the answer is. Can you help me determine the answer? Oh no! When you click on an image the browser follows the link. Fix it. Important: The code you write in each task should be added to the code written in the previous task.

1 //Problem: User when clicking on image goes to a dead end 2 //Solution: Create an overlay with the large image - Lightbox 3 ? 4 var $overlay = $('<div id="overlay"></div>'); 5 var $image = $("<img>"); 6 var $caption = $("<p></p>"); 7 ? 8 //An image to overlay 9 $overlay.append($image); 10 ? 11 //A caption to overlay 12 $overlay.append($caption); 13 ? 14 //Add overlay 15 $("body").append($overlay); 16 ? 17 //Capture the click event on a link to an image 18 $("#imageGallery a").click(function(event){ 19 ? 20 var imageLocation = $(this).attr("href"); 21 //Update overlay with the image linked in the link 22 $image.attr("src", imageLocation); 23

24 //Show the overlay. 25 $overlay.show(); 26

27 //Get child's alt attribute and set caption 28 var captionText = $(this).children("img").attr("alt"); 29 $caption.text(captionText); 30 }); 31 ? 32 //When overlay is clicked 33 $overlay.click(function(){ 34 //Hide the overlay 35 $overlay.hide(); 36 });

?

1 Answer

The default behavior in the browser is to follow a link when clicked. However, we don't want to do that, we just want the image to appear while staying on the page. In that case, it would be important to prevent the default behavior of the browser...perhaps the preventDefault() method would be useful here :)