0

I have a custom module, this module has a form with an ajax submit callback

$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
    'callback' => 'mymodule_submit_driven_callback',
    'wrapper' => 'box',
),
'#value' => t('Submit'),
);

I into the callback function I need to call a JQuery function:

function mymodule_submit_driven_callback($form, $form_state) {
//call JQuery function here
}

How can I do this? Thanks.

3 Answers 3

1

As Jose Daniel said: use ajax_command_invoke() in your ajax callback:

$commands[] = ajax_command_invoke(NULL, "your_function");
$output = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($output);

Your JS-File:

(function ($) {
  $(document).ready(function(){
    $.fn.your_function = function() {
      // do cool stuff
    }
  });
})(jQuery);
1
  • There is no need to warp the code that define additional functions on $.fn in a $(document).ready(...), unless definition of these functions actually requires the DOM document to be ready. Commented May 17, 2015 at 14:17
0

You seem to be asking a question by proposing an answer ... Why again do you need to call a jQuery function from within in your callback?

Normally in D7 the ajax callback function creates a list of Commands to send back to the client and perform client side javascript using jQuery.

Did you read up on the Ajax framework used by Drupal?

#ajax['callback']: The callback to invoke to handle the server side of the Ajax event, which will receive a $form and $form_state as arguments, and returns a renderable array (most often a form or form fragment), an HTML string, or an array of Ajax commands. If returning a renderable array or a string, the value will replace the original element named in #ajax['wrapper'], and theme_status_messages() will be prepended to that element. (If the status messages are not wanted, return an array of Ajax commands instead.) #ajax['wrapper']. If an array of Ajax commands is returned, it will be executed by the calling code.

EDIT:

Also see the Ajax Examples in the Developer Examples module.

1
  • 1
    I need to make a call because I need to update some things in the look and feel of the website.
    – user16360
    Commented Apr 6, 2013 at 16:22
0

I solved it using ajax_command_invoke().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.