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

Prakhar Patwa
Prakhar Patwa
11,260 Points

i did not pass event parameter in the function, but still it is working. I am wondering how?

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

//Still it is working

2 Answers

Steven Parker
Steven Parker
231,275 Points

You're using the window.event global property.

If you have not defined anything using the name event, it will refer to the event property of the global window object.

Note that it is not good practice to rely on this being available. If you need the event object you should always pass it explicitly to your handler.

Hi Prakhar,

Essentially, what your code is saying is this:

Choose the <a> that are in <li> that are in the element that has an id of "imageGallery". When the user clicks on any of these <a>; prevent the default behavior, choose the attribute "href" within the <a>, and print it to the console.

You don't always have to pass parameters to a function.

Hope this helps!

Steven Parker
Steven Parker
231,275 Points

:question: I thought the question was about the event variable, not the href property.