Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 3

Method chaining instead of a function?

I'm unclear on why we can't use jQuery method chaining to hide the overlay at the end of this video like this:

$overlay.click().hide();

instead of creating an anonymous function. Any explanation would really help solidify what we're learning for me, thanks!

2 Answers

Milo Winningham
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Treehouse Staff

In the jQuery documentation for click, you'll see that the click method with no arguments will trigger a click event rather than attach an event handler.

When attaching an event handler, you specify what should happen after the element is clicked by providing a function that says how to hide the element. In your example, you're calling the hide method on whatever is returned by the click method, which will cause the element to be hidden immediately.

Excellent, thank you for helping clarify!