question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event ajax:complete does not get triggered after form-replacement

See original GitHub issue

Hey.

Today I upgraded from jquery-rails gem 1.0.16 to 1.0.17. I found out that the “ajax:complete” event is not always triggered anymore.

I prepared a simple Rails app to show the problem: https://github.com/easychris/jquery17-rails-ujs-broken

In application.js I want to trigger all ajax:complete events for existing and new forms (hence the .live call):

$(function() {
  $('form').live('ajax:complete', function() {
    alert('ajax:complete event triggered');
  });
});

I prepared two sample data-remote=true forms, one page using jQuery 1.6.4, the other 1.7. The ajax call simply returns javascript code to replace the existing form by another form (in real life the new form would contain error messages etc).

$('#test_form_container').html(' <%= j (render :partial => 'form')  %> ');

When using jQuery 1.6.4 the ajax:complete event gets triggered, but unfortunately not using 1.7. I found out that in jquery-ujs the line

element.trigger('ajax:complete', [xhr, status]);

gets called, but it doesn’t trigger any event. I guess because the form to which the ajax event belongs doesn’t exist anymore (as it got replaced by the new form). But it should work, as I use the .live event to bind the event to any form-tag. And it does work in 1.6.4.

Any ideas? I tried a couple of hours to find the error, without success. Maybe someone else already has an idea?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Reactions:2
  • Comments:36 (13 by maintainers)

github_iconTop GitHub Comments

11reactions
feliperaulcommented, Jun 4, 2017

@stayhero Why is this issue closed ? I’m on Rails 5.1 and still having this problem: ajax:success is never called if the original element is removed by the reponse.js.erb file.

7reactions
trantorLiucommented, Jul 9, 2014

I do this in my EJS to solve the problem:

setTimeout(function () {
  $('#test_form_container').html(' <%= j (render :partial => 'form')  %> ');
});

In this way, the html replacement will be delayed to the next time tick that happens after the ajax:* events.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails UJS ajax:complete not firing - jquery - Stack Overflow
Read this jquery-ujs issue: Event ajax:complete does not get triggered after form-replacement.
Read more >
.ajaxComplete() | jQuery API Documentation
The function to be invoked. Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all handlers that have been registered...
Read more >
Listen to AJAX complete event from a behavior - Drupal Answers
When I click it, it updates a bunch of stuff as expected which is fine. I was wondering if there is a way...
Read more >
Campaign Design Javascript API Reference
When the experience to be shown is the default content, you can skip content hiding, using a campaign script, since content replacement is...
Read more >
jQuery and Ajax Tutorial
You can get the response text via property xhr.responseText . The .always() is called back after the .done or .fail completes. It takes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found