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

Help with jQuery Lighbox

We are passing the parameter "event" to our function? Then we call the parameter in code block and pass it the "preventDefault" method. Yet, "Event" is a DOM object, more so than a functional parameter. So, why is "event" declared as a parameter of the function rather than it's own variable?

function(event) { event.preventDefault();

2 Answers

Hey Gregory,

The "event" function parameter is actually just a variable name that you can make up that holds certain event methods. The compiler knows that the first argument you give it in any event listener function will receive the event methods, regardless of its name. And since there is no protected keyword for "event", it can be used as a variable name.

The variable name "event" here doesn't have to be "event" either, since you can make it whatever you wish, so long as it's valid. It can be any valid variable name such as "myevent" or "gregory" i.e.

$("input[type=text]").keyup(function (gregory) {
  //If you press the Enter key
  if (gregory.which === 13) {
    gregory.preventDefault();
    alert("Hi gregory!");
  }
});

However, most often, you'll see this variable name be "event", "evt", or "e" in order to avoid confusion as to its purpose.

Mustafa Başaran
Mustafa Başaran
28,046 Points

Hi Gregory, preventDefault() does not take any parameter. It is a method that can be called on the DOM object since it cancels the default action of a browser event (in this case, the defaut action of the click event is loading the picture all by itself in an empty window upon clicking on the link... which we do not want.) For further info, please see jQuery documentation: http://api.jquery.com/event.preventdefault/ I hope that this helps. Best regards, Mustafa