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

Ryan B.
Ryan B.
3,236 Points

Overlay: not allowing me to click on image. Images are remaining in their greyed out state. Any suggestions?

Image should get selected and enlarged in center of the page.

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

$overlay.append($image);

$overlay.append($caption);

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


//Capture the click event on a link to an image

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

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

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

$overlay.click(function(){
  $overway.hide();

});

2 Answers

Jamie Perlmutter
Jamie Perlmutter
10,955 Points

just a small typo, also in

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

it should be

var captionText = $(this).children("img").attr("alt");
  $caption.text("$captionText");
Jamie Perlmutter
Jamie Perlmutter
10,955 Points

dunno why both of those wouldn't except the first JavaScript markdown, if you need any help let me know I hope that works for you. When I adjusted those two things mainly the overlay typo, it worked like a charm.

Jamie Perlmutter
Jamie Perlmutter
10,955 Points

so there are a couple of slight issues, the biggest one is this

$overlay.click(function(){
  $overway.hide();

it should be

$overlay.click(function(){
  $overlay.hide();