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 2

Christopher Lebbano
Christopher Lebbano
15,338 Points

Understanding capturing the click as an event. (function(event) {});

In this video, we captured an event, when any "#imageGallery a" element is clicked.

I want to make sure I understand this correctly. In the following code:

$("#imageGallery a").click(function(event) { event.preventDefault(); var href = $(this).attr("href"); $overlay.show();

The parameter of the anonymous function is (event). That name is totally arbitrary correct? And the actual variable within the function (called "event") is actually the mouse-click itself, right? So what the function is saying, is, "When you click on the object, a function happens that captures that click, then prevent the default action of that new object/event (the click is an object? Or a variable?).

So anyway, my main question is, what exactly is the variable "event" in the function? Is it the mouse click, or something else?

1 Answer

Great question!

A great way to understand what the "event" parameter does is to understand what "event" means by itself.

An event is something that happened, not just mouse clicks but also keyboard input, mouse input, screen taps (on an IPhone), etc.

So the "event" parameter describes where the user clicked, which key on the keyboard did the user input, etc.

I hope you understand a little more now :)

Good luck! ~alex