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 3

Overlay won't let me click the images.

The problem is that i can't click the images because the overlay kinda blocks the way of me clicking the image.

here is my javascript:

// Making the image lightbox.

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

$overlay.append($image);

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

//1. Capture the click event on a link to an image
  $("#second-content-wrapper figure a").click(function(event) {
    event.preventDefault();
    var imageLocation = $(this).attr("href");
    $image.attr("src", imageLocation);

    $overlay.show();
    //1.2 Update overlay with the image linked in the link
    //1.3 Get child's alt attribute and set caption
  });

//2. Add overlay
  //2.1 An image
  //2.2 A caption
//3. When overlay is clicked
  //3.1 Hide the overlay

here is the css of the overlay:

#overlay {
     width: 100%;
     height: 100%;
     position: absolute;
     background-color: rgba(0,0,0,0.7);
     top: 0;
     left: 0;
 }

please help.

2 Answers

Gabbie Metheny
Gabbie Metheny
33,778 Points

You need to hide your overlay, using display: none; in CSS, so that it doesn't appear until you've clicked on an image:

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

You also will want to add this code to the bottom of app.js (after your image click event handler) to hide the overlay once more when you click anywhere on it:

$overlay.click(function() {
  $overlay.hide();
});
davidkomando
PLUS
davidkomando
Courses Plus Student 9,017 Points

Not sure where this comes into play even after referencing the video: "#second-content-wrapper figure a"