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

Is 'event' special?

The code below mostly makes sense to me.

    ('ul a').click(function(event){
    event.preventDefault();
    var href= $(this).attr('href'); 
    console.log(href);   
     });

Where I'm confused is the parameter we're giving the function. I understand function(event) to mean that 'event' is the parameter you put in the function when you call it. Is the same true for jQuery?

I'm confused because where in the code are we calling the function?

Could I replace event with the word 'red' or anything else? What exactly is the event we're appending '.preventDefault(); to?

1 Answer

The parameter name event is not exclusive on event methods. You can rename the parameter to any name you choose but just know that standard is usually event or e for short.

The function gets called when the event is created. when you click on whatever element is targeted then the event function gets called.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Do we really need to write a parameter for the function? It seems to work even without one. What "event" means? Or better said, what is "event" equal to since I don't see it define anywhere in the code (I remember when creating functions in JS, the parameters were defined before/ or after the function was created, but they were defined?