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 jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 4

Valerie Smith
PLUS
Valerie Smith
Courses Plus Student 5,244 Points

My caption is not showing up and I followed every step of the video! I could use another pair of eyes.

I followed every step as instructed and the image caption is not showing up. Please take a look at my code and let me know how i messed it! :)

//Problem: User when clicking on image goes to a dead end // Solution: Create an overlay with the large image- lightbox

var $overlay =$('<div id="overlay"></div>'); var $image = $("<img>"); var $caption = $("<p></p>");

//Add image to overlay

$overlay.append($image);

// caption

$overlay.append($caption);

// Add overlay

$("body").append($overlay);

//1.Capture Click event on link around image

$("#imageGallery a").click(function(event){ event.preventDefault(); var imageLocation = $(this).attr("href");

// Update overlay with the image linked in the link

$image.attr("src", imageLocation);

//show overlay $overlay.show(); });

//Get child's alt atribute and set caption

var captionText= $(this).children("img").attr("alt"); $caption.text(captionText);

// When overlay is clicked

$overlay.click(function(){

//hide overlay

$overlay.hide(); });

link: https://teamtreehouse.com/workspaces/22966492

Steven Parker
Steven Parker
231,269 Points

:x: You can't share a direct URL to your workspace, it's temporary and only exists while you are using it.

:information_source: But you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Steven Parker
Steven Parker
231,269 Points

:point_right: You may have a couple of code lines in the wrong place.

In the video, the code to "Get child's alt attribute and set caption" is placed inside the click handler for the gallery links, but in your code above you have placed those lines below and outside of the click handler.