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

The Lightbox hasn't been added, I can't find a bug in my code. I'm using chrome. I've tried safari and it's the same.

//Solution: Create an overlay with the large image - Lightbox

  // Add overlay


var $overlay = $("<div id = "overlay"></div>"); /*will be using more than once for showing and hiding so we make it a variable, that adds
it to document*/
var $image = $("<img>"); //declares image that will be appended to the overlay
var $caption = $(<p></p>);


 // adds an image to overlay
$overlay.append($image); //adds image before overlay is added, so it will be inside overlay by time overlay is added in the next line

// A caption to overlay
$overlay.append($caption);

$("body").append($overlay); //create overlay, and add to document


//1. Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
  event.preventDefault(); /* prevents browser from over riding code below in chrome developer tools*/
  var imageLocation = $(this).attr("href");
  //Update overlay with the image linked in the link
  $image.attr("src", imageLocation); // update source for image in overlay



  $overlay.show();// Show the overlay.

  // Get child's alt attribute and set caption

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


});

// When overlay is clicked
$overlay.click(function() {
  $(this).hide(); 
});
  // Hide the overlay
Katie Jordan
Katie Jordan
7,842 Points

var captionText = $(this).children("<img>") ^ try using just "img" to keep it consistent with other parts of your code!

5 Answers

I'm running into the same issue. I'm not sure what the problem is. I know this doesn't help, but at least you know you're not alone.

Jesus Mendoza
Jesus Mendoza
23,289 Points

Hey Ethan,

You forgot to add quotes to

var $caption = $("<p></p>");

Thanks for pointing that out. The output still hasn't changed though. I wonder if it could be a quirk with the browser?

Jesus Mendoza
Jesus Mendoza
23,289 Points

You forgot to use single quotes here too!

var $overlay = $('<div id="overlay"></div>');

If it doesnt work check if you have all the requiered HTML elements. Good luck!

Rick Hoekman
Rick Hoekman
9,494 Points

What Ethan said should be correct.

Could be the browser cache playing tricks.. Did you clear it?

Yes I did and it didn't change the result. I even tried incognito mode which has a fresh cache. Same result. I cleared the cache, the browsing history, downloads and cookies.

Rick Hoekman
Rick Hoekman
9,494 Points

Strange.. You are a bit ahead of me in the same course .. I'll post my code if it works or even if I encounter the same problem so we can compare.. Will probably this weekend.

Rick Hoekman
Rick Hoekman
9,494 Points

Try replacing

$(this).hide(); with $overlay.hide();

//When overlay is clicked
$overlay.click(function(){
  //Hide the overlay
  $overlay.hide();
});