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

Colin Taylor
Colin Taylor
3,341 Points

Can't get caption to show up!

Been through it several times, but can't find the problem w/ my code! Thanks in advance.

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

//add image to overlay
$overlay.append($image);
 // caption
$overlay.append($caption);
// Overlay
$("body").append($overlay);

//1Capture the click event on a link to an image
$("#imageGallery a").click(function (event){
 event.preventDefault();
  var imageLocation = $(this).attr("href");
//1.2 Update overlay w/ the image linked in the link

  $image.attr("src", imageLocation);
  //1.1. Show Overlay
  $overlay.show();
});

  //1.3 Get child's alt attribute set as caption
var captionText = $(this).children("img").attr("alt");
$caption.text(captionText);
//when overlay is clicked
  //3.1 hide overlay
$overlay.click(function(){
  $overlay.hide();
});

not sure what's the deal w/ the highlighting, sorry!

1 Answer

Hi Colin,

The 2 lines of code that you have that are dealing with the caption are outside of the click handler.

You should move them up so they're inside the click handler and put them before the line that shows the overlay.

Yay! I had this same issue, and your answer worked for me as well!