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 Perfect

Evan Gatchell
Evan Gatchell
5,878 Points

Functions as arguments to other functions.

$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent);
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

So these lines of code execute other functions when the element is focused and every time a key is released.

Why don't I include a parentheses with the function name? Like:

.keyup(confirmPasswordEvent());

??

1 Answer

Henrik Hansen
Henrik Hansen
23,176 Points

If you try to use a function as argument as this

.keyup( confirmPasswordEvent() );

the confirmPasswordEvent() will be executed when the code is loaded and not on the Keyup event. This is why you have to pass the function as argument without parentheses.

If you need to pass a function with arguments, then you need to create an anonymous function as the argument to the event, like this

.keyup( function () {
    confirmPasswordEvent( arg );  
);

Also, a reminder that parentheses are required to call the Function inside of conditions for If statements. I was just fumbling around for a while wondering why my Conditional Functions weren't working, and not calling with parentheses was the culprit.