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 2

Yvette Solorzano
Yvette Solorzano
3,080 Points

Black overlay isn't showing

Thanks for your help!

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>");
//Add overlay
$("body").append($overlay);
  //An image
  //A caption

//1. Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
  event.preventDefault();
  var href = $(this).attr("href");
  $("#overlay").show();
  //1.1 Show the overlay.
  //1.2 Update overlay with the image linked in the link
  //1.3 Get child's alt attribute and set caption
});

//3. When overlay is clicked
  //3.1 Hide the overlay

style.css

/** Start Coding Here **/
#overlay {
  background: black;
  width 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left 0; 
}

index.html

<html>
<head>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
    <title>Image Gallery</title>
</head>
<body>
    <h1>Image Gallery</h1>
    <ul id="imageGallery">
        <li><a href="images/refferal_machine.png"><img src="images/refferal_machine.png" width="100" alt="Refferal Machine By Matthew Spiel"></a></li>
        <li><a href="images/space-juice.png"><img src="images/space-juice.png" width="100" alt="Space Juice by Mat Helme"></a></li>
        <li><a href="images/education.png"><img src="images/education.png" width="100" alt="Education by Chris Michel"></a></li>
        <li><a href="images/copy_mcrepeatsalot.png"><img src="images/copy_mcrepeatsalot.png" width="100" alt="Wanted: Copy McRepeatsalot by Chris Michel"></a></li>
        <li><a href="images/sebastian.png"><img src="images/sebastian.png" width="100" alt="Sebastian by Mat Helme"></a></li>
        <li><a href="images/skill-polish.png"><img src="images/skill-polish.png" width="100" alt="Skill Polish by Chris Michel"></a></li>
        <li><a href="images/chuck.png"><img src="images/chuck.png" width="100" alt="Chuck by Mat Helme"></a></li>
        <li><a href="images/library.png"><img src="images/library.png" width="100" alt="Library by Tyson Rosage"></a></li>
        <li><a href="images/boat.png"><img src="images/boat.png" width="100" alt="Boat by Griffin Moore"></a></li>
        <li><a href="images/illustrator_foundations.png"><img src="images/illustrator_foundations.png" width="100" alt="Illustrator Foundations by Matthew Spiel"></a></li>
        <li><a href="images/treehouse_shop.jpg"><img src="images/treehouse_shop.jpg" width="100" alt="Treehouse Shop by Eric Smith"></a></li>
    </ul>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>

</body>
</html>
Yvette Solorzano
Yvette Solorzano
3,080 Points

Sorry if you can't read the code lol I tried very hard to use the markdown stylesheet to format. Let me know whatever is difficult to read I'll just paste it as a comment.

4 Answers

Hi Yvette,

You have a typo in your CSS; you're missing a colon between width and 100%.

Also, I edited your code for you so that it's legible. Here's a link that explains how posting code in the forum works.

Yvette Solorzano
Yvette Solorzano
3,080 Points

Thank you!!!

I commented on the bottom with updated code and I figured out how to format! Thanks for fixing mine :)

Yvette Solorzano
Yvette Solorzano
3,080 Points

And double thanks for the CSS check - I went ahead and added the colon but still no overlay :(

Oh I see, it's this line too:

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

You need to change your inner quotations to single-quotes. Either that or escape them with backslashes. I would go with the single quotes:

var $overlay = $("<div id='overlay'></div>");
Yvette Solorzano
Yvette Solorzano
3,080 Points

Hi! Changed the inner quotations to single quotes!

Still no overlay. I'm wondering if it's my browser?

var $overlay = $("<div id='overlay'></div>");
//Add overlay
$("body").append(overlay);
  //An image
  //A caption

//1. Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
  event.preventDefault();
  var href = $(this).attr("href");
  $("#overlay").show();
  //1.1 Show the overlay.
  //1.2 Update overlay with the image linked in the link
  //1.3 Get child's alt attribute and set caption

});

No, you just made another typo while fixing your mistake. You're appending "overlay" instead of "$overlay" to the body.

Yvette Solorzano
Yvette Solorzano
3,080 Points

Yey!!! That fixed it!

Thanks!!!!!!!! :))))

Hello. You have used the pure javascript method to get the element by id. To use the jQuery try var $overlay =$('#overlay');

Yvette Solorzano
Yvette Solorzano
3,080 Points

Hi! Thanks!

So this is what I've changed and still no overlay:

var $('#overlay') = $("<div id="overlay"></div>");
//Add overlay
$("body").append(overlay);
  //An image
  //A caption

//1. Capture the click event on a link to an image
$("#imageGallery a").click(function(event){
  event.preventDefault();
  var href = $(this).attr("href");
  $('#overlay').show();
  //1.1 Show the overlay.
  //1.2 Update overlay with the image linked in the link
  //1.3 Get child's alt attribute and set caption

});```

No. This is incorrect. She can't grab the element #overlay when it doesn't even exist in the DOM yet.

var $overlay =$('#overlay');

This doesn't make any sense Deni. This tries to select an element with the id of "overlay" from the DOM — except, this element has not been created yet. This is wrong.

Sorry, Mikis. Yes she needs to create an element with id overlay