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

Travis Barry
Travis Barry
9,020 Points

I know this is a problem for a lot of people but... I can't get the background to turn black :(

I am having a problem with getting the background to turn black when I click on the links :( I watched the video a dozen times and I looked at a lot of the questions but still nothing, I don't know what the problem is but if anyone cares to take a look and if you see anything wrong with the code I would really appreciate the feedback :) Even if someone can confirm the code works for them I will feel better about moving on :) Thank you all :)

Here is the jQuery code

//Problem: User when clicking on image goes to a dead end
//Solution: do the facebook, create an overlay with the large image - Lightbox

var jOverLay = $('<div id="overlay"></div>');
//2. Add overlay
$("body").append(jOverLay);
  //2.1. Add an image
  //2.2. Add a caption

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


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

Here is the CSS

body {
    font-family: sans-serif;
    background: #384047;
}
h1 {
    color: #fff;
    text-align: center
}

ul {
    list-style:none;
    margin: 0 auto;
    padding: 0;
    display: block;
    max-width: 780px;
    text-align: center;
}
ul li {
    display: inline-block;
    padding: 8px;
    background:white;
    margin:10px;
}
ul li img {
    display: block;
}
a {
    text-decoration: none;
}
/** Start Coding Here **/
#jOverLay {
  background:black;
  width: 100%;
  height: 100%;
  position: absolute;
  top:0;
  left:0;
}

And here is the HTML

<!DOCTYPE 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>

Thank you all very much :)

Travis Barry
Travis Barry
9,020 Points

Oh! Also I already tried both "jOverLay.show();" and "$("jOverLay").show();" in the function code and in the code declaring the variable and no luck :(

4 Answers

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

Here you are creating a div with an ID overlay.

In your CSS you are selecting something with the id of jOverLay. You need to change one of the names so they match.

#jOverLay {
  background:black;
  width: 100%;
  height: 100%;
  position: absolute;
  top:0;
  left:0;
}
Sage Elliott
Sage Elliott
30,003 Points

It looks like you're adding the ID to the div in div id="overlay" but in your CSS you have the ID #jOverLay instead of just #overlay.

akak
akak
29,445 Points

Hi.

In your CSS change #jOverLay to the id you gave the element before appending it - that is - #overlay.

'<div id="overlay"></div>'
Travis Barry
Travis Barry
9,020 Points

Oh my gosh! Thank you! Thank you thank you thank you! I kinda feel like an idiot for that but I'm so happy that problem stumped me for more then an hour and it works! Thank you :)