In a form, using #ajax to add a callback when a button is pressed. When the button on my form is pressed, I wish to call some JavaScript when the button is pressed and once again when the AJAX call is complete. can anyone suggest?
-
Is the ajax call under your control?– SGhoshCommented Feb 1, 2013 at 11:54
-
A similar question - stackoverflow.com/questions/4898381/…– SGhoshCommented Feb 1, 2013 at 11:55
-
And this too - stackoverflow.com/questions/11219706/…– SGhoshCommented Feb 1, 2013 at 12:03
-
Yes Ajax under my control– user13134Commented Feb 5, 2013 at 14:09
-
This looks like a duplicate of drupal.stackexchange.com/questions/9920/… which even provides a working example.– Adam BalsamCommented Jul 31, 2013 at 19:43
Add a comment
|
1 Answer
The "duplicate" I linked to above gives you almost all of your answer, but this adds the "complete" function that you asked for.
In a custom js file in your module (this assumes that your module is named ajax_example
and that the ID of the element being clicked is #edit-submit
):
Drupal.behaviors.ajax_example = {
attach: function (context, settings) {
// Overwrite beforeSubmit
Drupal.ajax['edit-submit'].options.beforeSubmit = function (form_values, element, options) {
console.log('before');
// Add your on click code here
}
// Overwrite complete
Drupal.ajax['edit-submit'].options.complete = function (form_values, element, options) {
console.log('after');
// Add your on complete code here
}
}
}