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 Password Confirmation Form Perform: Part 1

Robin Dykema
Robin Dykema
10,026 Points

If passwordEvent is a function, why doesn't it need () after it?

If passwordEvent is a function, why doesn't it need () after it?

For example, $("#password").focus(passwordEvent).keyup(passwordEvent); is correct but $("#password").focus(passwordEvent()).keyup(passwordEvent()); is incorrect.

Why?

3 Answers

the focus function is aware that you are passing in another function even though you dont include the () and will bind it to the event correctly

as I said, if you include the () you are calling the function at the exact point that the code is read, and at that point you do not want to execute it, you only want to bind it

consider this code

var focus = function ( functionName ) {
      functionName();
};

focus(someFunction);

so you see, here we can call the focus function, pass it the name of another function without the brackets, and it will still execute the function

because when you add () to a function name, you call the function. so when you run the code the function will run, but you dont want that, you only want the function to be binded to those events

Robin Dykema
Robin Dykema
10,026 Points

Don't I want to run it when the other events occur? When the focus() function occurs, don't I want my function to occur?