How to programmatically trigger a click on an AJAX enabled form submit button?
I'm trying to programmatically (with jQuery) trigger a click on button with AJAX behavior in a Drupal form, but so far jQuery('#edit-submit').click()
doesn't do anything.
A real mouse click on that button works as intended. Any ideas how to make it work?
Answers 7
jQuery('#edit-submit').mousedown()
- apparently there's a big difference.Actually, there's no need to guess.
You should use Drupal behaviors
This will give you access to the settings' ajax property,
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.
Create ajax submit like the following.
Then the jquery .click() event would be work in the drupal ajax form.
You can also use
.trigger()
jQuery method.$('#element').trigger('click');
In my case, the above recommended solutions didn't work for me, but the mention of .mousedown() led to the following idea that worked for me (Drupal 7):
There's some helpful background info about "why" this is the case in the Form API Reference under #ajax_prevent
Looking at the Better Exposed Filters module, they submit the AJAX form by finding $(.ctools-auto-submit-click') and trigging a click.
You have to trigger the
submit
event on the form.Click
andmousedown
events on buttons are not working.