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 1

Josh Bennett
Josh Bennett
15,258 Points

PreventDefault() again.....

Its just not working. I have tried twice, cleared browser cache, chrome and safari, workspaces and Atom. No Dice.

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

//1. Capture the click event on a link to an image $("#imageGallery a").click( function( event ){ event.preventDefault(); var href = $(this).attr("href"); console.log();

//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

});

<!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.3.min.js" type="text/javascript" charset="utf-8"></script> <script src="js/app.js" type="text/javascript" charset="utf-8"></script>

</body> </html>

Josh Bennett
Josh Bennett
15,258 Points

Now the link images dont even work so i just broke it .

Maciej Sitko
Maciej Sitko
16,164 Points

Please provide us with your full javascript code.

I have been running into the same problem. I'm convinced there is something up with the workspace. When I download the files and work in a text editor it works fine. If I copy that same exact code into the workspace, it fails. The weird thing is that it is working for some people and not for others. So I guess the best solution would be to work in a text editor, which is probably where we should all be doing this stuff anyway.

5 Answers

Maybe you've already figured it out, but the code you posted above needs the href passed to the console.log() function:

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

//1. Capture the click event on a link to an 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 child's alt attribute and set caption

});

Maciej Sitko
Maciej Sitko
16,164 Points

The solution for the task you linked to is this anyway:

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

//1. Capture the click event on a link to an 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 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
Josh Bennett
Josh Bennett
15,258 Points

The solution doesn't help me. Giving me the answers doesn't show me why mine didn't work. What I posted was the full JavaScript to that point in the project. The same point everyone else was having trouble.

Maciej Sitko
Maciej Sitko
16,164 Points

But there really wasn't much to it, even to explain. You have two steps here to accomplish the task, preventDefault and assign href attr to href variable.

And your code works, it passes both steps, all i've done is reformatted it.

Josh Bennett
Josh Bennett
15,258 Points

Yeah, I knew there wasn't much to it. But it wasn't and still doesn't work for me. That's why it so frustrating. Apparently I'm not the only one with this issue.

Carey LaMothe
Carey LaMothe
5,312 Points

I'm struggling with this issue as well. It's a big time waster and still no answers. I'm wondering, can anyone recommend an alternative text editor?

Josh Bennett
Josh Bennett
15,258 Points

Thanks, I suppose I'll just move on.