drupal-7drupal-formsdrupal-ajax

Drupal: How to bind a custom jQuery function to a certain AJAX response?


I have an AJAX form which is validated by AJAX callback function. Everything is done using Drupal API. But I don't want to use standard mechanism of appending/prepending/replacing of DOM elements when callback happens. How to catch that AJAX response and react on it with my custom function?


Solution

  • You can try defining a behavior. In many situations, including an AJAX request, Drupal will call to "attachBehaviors" that will trigger your behavior too:

    Drupal.behaviors.mybehavior = {
      attach: function (context, settings) {
    
              console.log(context);    
    
    
      }
    };
    

    You must check and inspect context to know if it's the right context what are you looking for