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

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Why did we give the function an "event" parameter?

As far as I can see, the function works exactly the same even if I don't assign a parameter to it. What exactly is "event", a function, a variable?

Well I'll be, it works for me as well without the parameter. I'm gonna post that question and see what floats to the top.

I posted the question about prevent default working without a parameter. I'm guessing the answer that will come back is "It's best practices". Which means everyone does it that way so that's how we do it.

So far what I've found out is that in THIS circumstance it doesn't matter if we pass in a parameter. I haven't found out yet in what circumstances we MUST pass in a parameter or why.

Arikaturika, I don't think you were sleeping and missed it. There is a lot of information that is presented in the videos. I guess they have to pick and choose which bits to go in more depth. Otherwise every video would be days long. At least that's my current thought on the subject having been through the whole front end course several times now.

4 Answers

I came across this from an ajax course that Dave teaches. It seemed to add a bit more insight.

//Evt here is an argument that jQuery passes to all event handlers.
//Click, mouse over, submit and so on.
//It represents what's called an event object.
//It has its own special properties and methods.
//In this case, we're interested in the preventDefault method, which prevents
//a browser from completing its normal action in response to an event.
//You can use it with clicks on links to prevent
//the browser from following a link and leaving the page.
//Or you can use it on a submit event like this to
//keep the browser from exiting the webpage when a user submits a form.
Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Oh... I understand. I find it strange this wasn't explained in the video. Or was it and I was sleeping (which is also possible)? Maybe this particular argument will be introduced later in the course. Thx for the detective work :)!

if you are referring to this

$("img href").click(function(event){
  event.preventDefault();

});

event is a parameter, which acts just like a variable. If I remember correctly..the click would bring you through to the img at its source. Which is the default behavior for clicking on a link. preventDefault() is a particular method to stop that from happening. As to it working without that parameter, I guess that would depend on what you mean by working. Are you saying that without that parameter, preventDefault still works?

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Ok, let me rephrase: what is the event parameter? What is its value? Event = click in this case? Or event = the action of going to the source of the image?

Yes, without the parameter, preventDefault still works. Thank you!

That is a great question. I looked THAT question up on w3schools and this is the answer they gave: "The event parameter comes from the event binding function". So maybe you can explain to me what that means, cause I only have this vague notion of what it might mean. I THINK its saying that we are telling the function that we are giving it some information. (I guess like we always do with parameters) and the information we are giving it is: don't do what you're designed to do. So preventDefault is a method like alert or console.log. By typing that particular string of characters we pass in all the programming behind it. Now I'm gonna try that bit of code without the parameter and see what happens.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

john larson I am more confused than you are. Maybe I'll re post the question and ask for a reply from the instructor. The code works indeed without or with a parameter, so I don't know what's the role of "event" or what does it represent. I understand the rest of the code, except for this bit.

Thank you for the help :)!

So far what I've found out is that in THIS circumstance it doesn't matter if we pass in a parameter. I haven't found out yet in what circumstances we MUST pass in a parameter or why.