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

CSS jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 1

Oliver Sewell
Oliver Sewell
16,425 Points

preventDefault not working ! -.-

//problem: user when clicking on image goes to a dead end.
//solution: create an overlay with the large image.- a lightbox.

//1. capture the click event on a link to the image.

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


       //1.1 show the overlay.
 // 1.2 update overlay with the image linked in the link
// 1.3 Get childs alt attribute and set captio



 //2 Add overlay
 //2.1 an image
//2.2 a caption

//3. when overlay is clicked 
    //3.1 hide the overlay 
<!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>
Oliver Sewell
Oliver Sewell
16,425 Points

have also tried $("#image Gallery a") with a space but still not working T__T help

huckleberry
huckleberry
14,636 Points

You went from ImageGallery in your html which is different spelling from imageGallery which is used in the jQuery call to "image Gallery" which is actually more wrong lol.

The correct spellilng is imageGallery. ImageGallery is wrong because it has a capital I in image and image Gallery is wrong because that's TWO words. By typing class="image Gallery" you're literally giving it two classes.

//notice the spelling. One word... lowercase i
$("imageGallery a");
<!-- mistake #1 you capitalize the letter I -->
<ul class="ImageGallery"></ul>

<!-- mistake #2 you make one word into two thereby creating two separate classes -->
<ul class="image Gallery"></ul>

<!-- only correct way in regard to making it the same as the JS spelling ... one word, camelCase -->
<ul class="imageGallery"></ul>

Cheers,

Huck - :sunglasses:

4 Answers

Brian DuPont
Brian DuPont
30,448 Points

I noticed the div id has a capital "I" and "G", but your jquery selector only has the "G" capitalized.

Abe Layee
Abe Layee
8,378 Points

Your code is correct, but for the challenge, you're not asked to console.log() href.

Oliver Sewell
Oliver Sewell
16,425 Points

its not a challenge, i was following along in workspaces but the preventDefault(); doesnt seem to be working

I had the same problem for a long time. I can't explain why, but here is what worked for me… I changed the quote marks that are around $("#imageGallery a") from double quote marks to single quote marks $('#imageGallery a') and it worked. Even more bizarre is that I changed them back to double and it still works.

James N
James N
17,864 Points

i noticed when i changed it from double to single it changes some of the text colors. and those new colors are there when i switched back..... also i didn't know that the hashtag was mandatory... Thanks!

I also had this same problem and all I did is add two spaces between "imageGallery" and "a". Like this...... $("#imageGallery a")