0

Currently this function works:

    $("#email_address_input").on('focusout', function(){
        emailValidationCheck($(this));
    });

    function emailValidationCheck(e){
        ...
    }

So basically, if the email address input element is focused out, then an anonymous function runs, which calls the declared function emailValidationCheck (and of course, that declared function takes as an argument the email address input element).

That anonymous function feels redundant. All it does is call the declared function, so it seems to me like it should be taken out.

So, what I tried to do was call the declared function directly upon the event firing, as opposed to calling the anonymous function, which in turn calls that declared function. Like this (warning, it doesn't work as expected):

    $("#email_address_input").on('focusout', emailValidationCheck($(this)));

Question: How can I get this to work? Or is the original answer best practice? Basically what I am trying to do is: when the focusout event fires off on the specified element, I want to execute the emailValidationCheck function, where the passed in argument is the element where this all this stuff is happening on.

Thanks!

1
  • you could paste everything from the emailValidationCheck function to the anonymous function in the .on() Commented Mar 9, 2015 at 13:59

2 Answers 2

4

You don't need to use anonymous functions as callbacks for events. You can easily call a defined function without using the () precursor (because including that will essentiall pass the return value of emailValidationCheck to the callback, rather than the function reference itself). For example:

$("#email_address_input").on('focusout', emailValidationCheck);

Now, your emailValidationCheck function will receive the event in the e variable that you define in the function constructor.

Because the function has been bound as a callback, $(this) is also available within it. For example:

function emailValidationCheck(e)
{
    console.log( e );       // logs the event
    console.log( $(this) ); // logs the jQuery object that lost focus
}

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

2 Comments

what about the argument for the emailValidationCheck function? Doesn't it have to be specified when I call it? How can I refer to that argument when I am within the emailValdiationCheck function?
@Neil No you don't. jQuery automatically passes the event to the callback function, and passes the this variable to it, so it's within the scope of the callback. I was editing the answer when you commented. Please see the update.
1

That's not how javascript works. The .on() function wants a function as a parameter. You can either pass an anonymous function or the name of a function. As soon as you put () at the end, it executes the function inline and passes the result to the .on() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.