arvinsim arvinsim - 6 months ago 68
jQuery Question

jquery.form.js - How to get current form on submit?

I have this markup

<form action="..." id="form1" class="foo">...</form>
<form action="..." id="form2" class="foo">...</form>

and I have this Javascript code

dataType: 'json',
success: function (data) {
// TODO: How do I get the form that triggered this?
console.log( $(this) );

As you can see in the Javascript code, I want to get the current form element when
it submits and was succesful. Outputting
didn't show me anything that points
to the current form element.

How do I get the current form element inside the success function?


Assuming you are using jQuery Form Plugin

You can use 4th parameter

 success: function (data, statusText, xhr,  form ) {


success: Callback function to be invoked after the form has been submitted. If a 'success' callback function is provided it is invoked after the response has been returned from the server. It is passed the following arguments:

  1. responseText or responseXML value (depending on the value of the dataType option).
  2. statusText
  3. xhr (or the jQuery-wrapped form element if using jQuery < 1.4)
  4. jQuery-wrapped form element (or undefined if using jQuery < 1.4)