Drupal - How to programmatically trigger a click on an AJAX enabled form submit button?

jQuery('#edit-submit').mousedown() - apparently there's a big difference.


Actually, there's no need to guess.

You should use Drupal behaviors

Drupal.behaviors.yourFunctionName = {
    attach:function (context, settings) {

       // Your code goes here....

    }
}

This will give you access to the settings' ajax property,

Drupal.behaviors.yourFunctionName = {
    attach:function (context, settings) {

       console.log(settings.ajax);

    }
}

Depending on your configuration, you should see a list of triggering elements, with various properties such as the name of the callback function, the selector's id as well as the name of the triggering event.

You can then use the relevant information to trigger your event.

// $(selector).trigger(eventName);
// for example...
$('#edit-product-id-15', context ).trigger('change');

Create ajax submit like the following.

 $form['button'] = array(
    '#type' => 'button',
    '#value' => 'Click',
    '#ajax' => array(
      'callback' => '_kf_reading_user_points',
      'wrapper' => 'reading-user-points',
      'method' => 'replace',
      'event' => 'click',
    ),
  );

function _kf_reading_user_points(&$form, &$form_state) {
  // Something within the callback function.
}

Then the jquery .click() event would be work in the drupal ajax form.

Tags:

Javascript

Ajax

7