Callback after Ajax form submit by Drupal 8 form

Viewed 1279

I have one Drupal 8 form (ID: regform) and I have to done some JS triggers after the form AJAX submit.

For example form contains choose plan field, after form submit, I have to display some calculated price in the other block in the same page, So I am trying to pick a callback function after Ajax submit. What I have to do ?

2 Answers

I hope you can use the below code to resolve your issue. You can add this snippet to a javascript file which located in your custom theme or module.

(function ($, Drupal) {
  Drupal.behaviors.custom = {
    attach: function (context, settings) {
      $(document).ajaxComplete(function (event, xhr, settings) {
        if (settings.data && settings.data.indexOf("form_id=regform") != -1) {
          //your code here
        }
      });
    }
  }
})(jQuery, Drupal);

This worked for me in Drupal 9.2.10: (December 2021)

jQuery(document).ajaxComplete(function(event, xhr, settings) {
  if (settings.data.indexOf( "form_id=modal_test_form") != -1) {
    console.log('Hello!');
  }
});
Related