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

HTML jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 2

Michael Russell
Michael Russell
7,332 Points

event.preventDefault(); Not Working in Mozilla Firefox

Hello, I am working on "Perform: Part 2", creating the image overlay light-box for the JavaScript course and the behavior is not matching the example in the video. For example, when I click on the image, the image link is opened even though the event.preventDefault(); method is supposed to prevent this from happening. I'm using the most recent version of Mozilla Firefox and the browser doesn't seem to recognize this code. My code includes the following:

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

$("body").append($overlay);

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

});

What can I do as I am working on my company laptop and I am not allowed to download Chrome?

Richard Duncan
Richard Duncan
5,568 Points

What message do you get when you click on the URL in this example?

Michael Russell
Michael Russell
7,332 Points

Richard, I get this response when clicking on the link in your example: 'preventDefault registered'

Michael Russell
Michael Russell
7,332 Points

//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>'); var $image = $("<img>"); var $caption = $("<p></p>");

//An image to overlay $overlay.append($image);

//A caption to the overlay $overlay.append($caption);

//Add overlay

$("body").append($overlay);

//A caption

//Capture the click event on a link to an image

$("#imageGallery a").click(function(event){

   event.preventDefault();
var imageLocation = $(this).attr("href");

//Update overlay with the image clicked on the link
$image.attr("src", imageLocation);

//Show the overlay

    $overlay.show();

//Get child's alt attribute and set caption
var captionText = $(this).children("img").attr("alt");
$caption.text(captionText);

});

//when overlay is clicked $overlay.click(function(){

$overlay.hide();

});

Richard Duncan
Richard Duncan
5,568 Points

Can you add your html to here please :)

Michael Russell
Michael Russell
7,332 Points

I added my html to the provided link. Do I need to click on anything to save it or anything so you can view it?

Richard Duncan
Richard Duncan
5,568 Points

Yeah click "Share" then click on the lock to lock the changes and send me the link or revision number and Ill take a look.

2 Answers

Richard Duncan
Richard Duncan
5,568 Points

You need to add the elements into the variables at the beginning of the code or jQuery doesn't know what to append the values to so you end up with nothing.

Example: -

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

There was some funky shit going on with your anonymous functions not being closed correctly too which is why it displayed the behaviour which made you think preventDefault was not working in FF. The first step we took was to debug preventDefault and determine if that method was functioning as we would expect it to do in FF using the defaultPrevented on the event which returns a boolean value of true if the method was called successfully.

Have a look at this example you should be able to pick it up from here, obviously it doesn't look quite right without images or CSS but you get the general idea. The overlay is at the bottom of the page so you may need to scroll.

Michael Russell
Michael Russell
7,332 Points

Thank you so much, Richard. I did my best to follow along closely with the training modules. I must have just mistyped some things and maybe the code in the training itself had some bugs in it as well. You were a huge help anyway, and I appreciate it greatly!

Richard Duncan
Richard Duncan
5,568 Points

Thanks man pleased I could help!

Michael Russell
Michael Russell
7,332 Points

Richard, I get this response when clicking on the link in your example: 'preventDefault registered'

Richard Duncan
Richard Duncan
5,568 Points

So preventDefault is working in your browser the issue must lay somewhere else. Could you post all your code?