Dónal
Dónal

Reputation: 187539

handling successful form submission with jquery

In my application I'm using a plugin that generates the following markup:

<form id="addCommentForm" 
  action="/foo/add" 
  method="post" 
  onsubmit="
    jQuery.ajax({type:'POST', data:jQuery(this).serialize(), 
      url:'/foo/add',

      success:function(data,textStatus) {
        jQuery('#comments').html(data);
      },           
    });
    return false">

<!-- form elements here -->
</form>

When the form is submitted successfully I want to do something else after the success handler defined by the plugin, say alert('hello');.

The reason I'm struggling with this is because I can't just add my code to the end of the success handler above, because this code is not under my control.

I looked for a form event that executes after onsubmit that I could attach my code to, but didn't find anything.

Upvotes: 0

Views: 2756

Answers (1)

Bruno Silva
Bruno Silva

Reputation: 3097

If you can't really change it, you could use .ajaxSuccess() to handle all the external ajax calls and filter the one you need:

$('form').ajaxSuccess(function(evt, request, settings) {
 if (settings.url == 'xxx')
     alert('test');
});

Not pretty but it might work for you.

Upvotes: 3

Related Questions