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 not working/how are you getting away with no src of your javascript nor cdn in html? Thanks

I tried it in both firefox and chrome, still goes through to another page when the link is clicked. Console.log("hey") works so javascript is connected properly. Tried in dreamweaver and workspace. Any help appreciated thanks.

<!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>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="js/app.js"></script>
</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>

</ul>
</body>
</html>
$("#imageGallery a").click(function(event){
   event.preventDefault();
   var href = $(this).attr("href");
   console.log(href);


});
Charles Smith
Charles Smith
7,575 Points

Is the js file called right? With the above HTML, your directory structure should be something like

my_proj/js/app.js

2 Answers

If I put a $(document).ready(function() { in front it works thanks for the help though, I'll leave this here in case anyone else has the same problem.

Joseph Anderson
Joseph Anderson
10,617 Points

I had to do this too. Thanks for figuring it out.

Anish Walawalkar
Anish Walawalkar
8,534 Points

try the following:

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