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
brevans26
15,198 PointsAnonymous function to log into the console or to pass an event?
Around 2 minutes of the video, Andrew suggests to print the code to the console to check eventual mistakes and etc. To do that, he creates and anonymous function by typing the following code:
$("#imageGallery a").click(function() {
console.log();
});
Later in the video, around 6 minutes, he finds a method to stop the default behavior of the browser. For doing that he passes an event inside of a function by typing the following snippet of code:
$("#imageGallery a").click(function(event) {
event.preventDefault;
});
I am just confused if the anonymous function was created to print things to the console or if it was created to pass an event later. If it was created just to pass an event in the future, why it was created earlier? If it was created to print things to the console, how does that affect this action?
Thanks!
1 Answer
Jeremy Cox
9,300 PointsI pretty sure the function is performing both roles. Logging the data to the console allowed him to see what data the click was pointing to. The event.preventDefault was placed there so that the click does not actually follow the url would have done otherwise.