0

I have an event binding that looks like this:

$('#form').on('submit', { callback : Obj.method }, FunctionName);

The binding runs on page load, however, at that time Obj.method is not defined -- I had thought that it used whatever the current value of Obj.method was, but it apparently uses whatever value it has at the time it runs.

Is there a way to have the data determined at the time the event handler fires? My guess at this point is no ... but people here seem to know a lot more then I do.

2 Answers 2

2

Just wrap the callback with a function and it won't be run until the callback fires.

$('#form').on('submit', {
    callback : function() {
        Obj.method();
    } 
}, FunctionName); 
Sign up to request clarification or add additional context in comments.

2 Comments

It seems that there is little point in using the data argument to the .on() function if you just want the data evaluated at the time of the event. So, if the OP wants the value of Obj.method() at the time of the event, then why pass the data argument like you're doing? It doesn't buy you anything at all. All it accomplishes is to put something into event.data that you can reach normally without putting it there. Isn't it more straight forward to just call Obj.method() directly from your event handler?
@jfriend00 I don't presume to know what the OP is trying to do, I'm just answering his question. I agree it makes little sense.
1

If it's a variable that exists at the time the event handler fires and you want it evaluated then, you can just reference it directly from the handler function:

$('#form').on('submit', function(e) {
    FunctionName(Obj.method);
});

or, if you just want it called directly:

$('#form').on('submit', function(e) {
    Obj.method();
});

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.