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 trialChris Baldwin
Full Stack JavaScript Techdegree Graduate 43,358 PointspreventDefault 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);
});
2 Answers
Chris Baldwin
Full Stack JavaScript Techdegree Graduate 43,358 PointsIf 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
10,617 PointsI had to do this too. Thanks for figuring it out.
Anish Walawalkar
8,534 Pointstry the following:
$("ul#imageGallery li a").click(function(event){
event.preventDefault();
var href = $(this).attr("href");
console.log(href);
});
Charles Smith
7,575 PointsCharles Smith
7,575 PointsIs the js file called right? With the above HTML, your directory structure should be something like
my_proj/js/app.js