Chris Chris - 1 year ago 53
jQuery Question

Cannot pass jQuery form.submit() to another function to be executed

I have the following JavaScript:

function MyMethod(func)

$('#Btn').on('click', function(e)

var form = $(this).parent();


This doesn't work, giving the following error in Chrome:

jquery.min.js:4 Uncaught TypeError: Cannot read property 'trigger' of undefined

If I modify the call to
to use the following then it works:


Can someone explain why this is?

Answer Source

Ah, good ol' binding of this at work! If you only provide the method, it will not be called on the form but on the global object, like // resp. undefined in strict mode. Hence the error, I guess.

where what you want is

One way to fix this: Bind the submit method to the form:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download