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

preventDefault(); will not work for me....

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

anyone know what is wrong with my code?

jerred poitra
jerred poitra
7,241 Points

I have the exact same problem. I've tried every fix here and it won't work. I even started from the beginning twice and still nothing shows up in the console. I don't even think I can progress any further because my workspace will be wrong in the following lessons!

14 Answers

Corin Ashwell
Corin Ashwell
919 Points

OK, go it. Hard refresh of the page, e.g. ctrl + F5, refreshes the HTML so modifications like the id of the ul work.

This worked for me.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

I updated your code so it displays correctly.

Your code works for me. Do you have a link to jQuery? Not having it is the only reason that I can see at the moment that it wouldn't work.

<!-- jquery included in the line below -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<!-- now this script should work -->
<script>
$('#imageGallery a').click(function(event){
  event.preventDefault();
  var href = $(this).attr("href");
  console.log(href);
});
</script>

Yeah I have linked it to JQuery, not sure why it will not work on my workspace.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

do you have an element with the ID of imageGallery? Just checking. If you DO have that and it's still not working can you take a snapshot of your workspace and share the link? If you don't know how let me know and I will tell you.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

what is it doing on your end? It seems to be working http://screencast.com/t/I8WgiTkcFk

That is so weird. It still continues to proceed to the link when I click on any of the images....

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Hmmm, interesting. Did you try refreshing your browser yet? The last time that I saw something weird like this I think it was fixed by refreshing the page

Yup, I tried refreshing, even closing out completely.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

If you want to let me know what browser you are using then I could try it in that browser and see if it's something with the browser. That's my only other idea.

I am using google chrome

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Yep, I use Chrome as well. Windows 10. I'm completely out of ideas on this one (beyond trying clearing your cache or trying to launch workspaces in a different browser). If you don't get it figured out and no one else answers then you might try re-posting the question (they might think this one has been answered and just not marked as answered).

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

If you do re-post then include the link to your workspace. That might be helpful. Maybe someone will see what we are looking over

jason chan
jason chan
31,009 Points

document ready wrap your code in this. If not put the code near end of the </body>

// A $( document ).ready() block.
$( document ).ready(function() {
    console.log( "ready!" );
})

I had some type of similar problem if I had multiple browser windows to the served page closing the window or refresh fixes ...

So I was able to fix it. I basically had to start over completely in a brand new workspace but now my code works for some reason. Thanks for all the help everyone!

Ryan Southard
Ryan Southard
2,406 Points

Totally maddening. I've tried everything here: Checked my code, rewritten it and checked it again, reloaded everything, I still go straight to the "dead end," without fail.

My code, unannotated:

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

This seems like a pretty important thing to nail. I'm reluctant to move on until I resolve this. Throw us a bone, Treehouse!

Josh Bennett
Josh Bennett
15,258 Points

I'm in the same boat.

Corin Ashwell
Corin Ashwell
919 Points

I have this problem. What I notice is that if I view source after previewing the workspace the ul does not have the id imageGallery. I assume this is why the jquery function on the id is not working. I haven't figured out how to correct this yet, any advice gratefully received!

Aaron Selonke
Aaron Selonke
10,323 Points

I also have know idea why mine it is not writting to the consule. Tried the ctrl + F5 and cleared the chrome browser cache.

Here is the screen shot. http://picpaste.com/pics/Captureup-c7ZVbrr8.1456343199.JPG

Derek Gella
Derek Gella
15,215 Points

Solution: update the jQuery link in the HTML file to: http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"> instead of what is pre-loaded in the workspace. The link that is pre-loaded does not work.