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

dont understand why this code isnt working ?

everything seems to be working except for the prevent default ? it still loading the image.

var $overlay = $('<div id="overlay"></div>');
$("body").append($overlay);

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

Hi simon lavery, I've updated your question so the code is correctly highlighted.

As for your question, you might need to share some more code surrounding just what you have there. (Such as the HTML for the links you're binding event handlers to, etc).

Note that you're also setting the variable href in that function, but not using it for anything.

2 Answers

Here's a quick Codepen that might illustrate the problem for you:

http://codepen.io/IAMZERG/pen/qOBmEK

I had the same issue. My problem was that even though i've added the id "imageGallery" to the ul in the index.html file, the workspace didn't update. Make sure to refresh (F5) the browser with the "workspace" preview, even if you have reopened it. Or check the source code if the element imageGallery is really added.