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

Shirley Xie
Shirley Xie
230 Points

My caption is not showing up and I believe I followed every step of the video...

Could someone kindly check out my code please? My caption is not showing up and I believe I followed every step of the video... Snapshot: https://w.trhou.se/hn6hu0gu3p

app.js

//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);

//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');

$overlay.text(captionText); });

//When overlay is clicked

$overlay.click(function(event){

//hide overlay

$overlay.hide(); });

style.css body { font-family: sans-serif; background: #384047; } h1 { color: #fff; text-align: center }

ul { list-style:none; margin: 0 auto; padding: 0; display: block; max-width: 780px; text-align: center; } ul li { display: inline-block; padding: 8px; background:white; margin:10px; } ul li img { display: block; } a { text-decoration: none; } /** Start Coding Here **/

overlay {

background:rgba(0, 0, 0, 0.6); width: 100%; height: 100%; position: absolute; left:0; top: 0; display: none; text-align: center; }

overlay img{

margin-top: 10%; }

overlay p {

color: white; }

3 Answers

Steven Parker
Steven Parker
229,788 Points

Line 25 should probably set the caption text.

Instead of:

  $overlay.text(captionText);

Line 25 should probably be:

  $caption.text(captionText);
Shirley Xie
Shirley Xie
230 Points

That's right! Thank you so much Steven!

Steven Parker
Steven Parker
229,788 Points

The workspace code is a bit different from that shown above.

I found the following lines in app.js in the workspace snapshot:

//Add image to overlay
$overlay.append($image);
// caption
//$overlay.append($caption);        <-- this line
// Add overlay
$('body').append($overlay);

The line that would add the caption (line 10) has been commented out. Remove the slashes (//) to activate it.

Shirley Xie
Shirley Xie
230 Points

Sorry I forked the wrong snapshot, now the link has been updated, I updated all the comments and it still doesn't work.

Shirley Xie
Shirley Xie
230 Points

What I also noticed that is: If I comment out line 25 from js: $overlay.text(captionText); , at least the image overlay worked, but when I uncomment that line, the image doesn't even append to the overlay anymore, it's just the shadow overlay, I am so confused why this is happening...