Toni Michel Caubet
Toni Michel Caubet

Reputation: 20183

Using on() to trigger a form submit

I am trying to bind this submit event from a form which not always be in Dom, so I trying with .on():

$('body').on("form","submit", function(e){})

but firebug logs:

$("body").on is not a function
[Parar en este error]

$('body').on("form","submit", function(e){

tried also

$(document).on("form","submit", function(e){})

Upvotes: 2

Views: 4273

Answers (2)

Niklas
Niklas

Reputation: 30012

If you are upgrading to jQuery 1.7, you can use on() by applying form as a selector, like:

$('body').on("submit", "form", function(e){

Which will bind the submit event to body, but filtering it to form which works the same way as the delegate example below.

If you are using 1.4.2 or newer, you can use delegate() like this:

$('body').delegate("form", "submit", function(e){

Upvotes: 9

Will
Will

Reputation: 20245

You have to select the form and use on to bind the event listener.

$( "form" ).on( "submit", function(e){} );

You also need jQuery version 1.7 or higher to use on.

Upvotes: 0

Related Questions