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 3

why??

Hi everyone. Why cant we use $overlay.click().hide(); instead of

$overlay.click(function() { $(this).hide(); }); ? must we use functions? and how can i know, whether i should use function or not?

2 Answers

Claudio Salatino
Claudio Salatino
4,887 Points

If I got your question right I think it's because click() is an event handler which requires a callback function as an argument to make sure a certain action is performed after an element is clicked. $(this) represents the element on which the event is triggered, in this case, the overlay.

Adam Pengh
Adam Pengh
29,881 Points

Hi Javid Abbasov,

A good way to think about it is in a cause and effect way. For example, when someone clicks, you want something to happen (a function). The function is the part that actually says what should happen. jQuery has made working with JavaScript much easier, but you still need to use functions in order for anything to actually happen.