jQuery: Trigger submit on ajax enabled form in Drupal

Published by Lennart Van Vaerenbergh on April 22, 2015

I was trying to trigger 'click' on an ajax enabled submit button when hitting Enter in a text field. Well that was a disappointment. Fear not, there's a solution. Just keep in mind that click() or trigger('click') is not going to work for us to fire off the form submit.

Drupal Form API way

Add the highlighted line to your textfield in the form array:
$form['element'] = array(
  '#type' => 'textfield',
  '#title' => 'Title',
  '#ajax' => array(
    'callback' => '...',
    'keypress' => TRUE,

Non Drupal Form API way

First of all, in our javascript file, let's create a function that provides us with an onEnter event. By default, your 'hit enter and submit' won't work on your ajax enabled form, so we'll define the 'hit enter' trigger ourselves.
$.fn.onEnter = function(func) {
  this.bind('keypress', function(e) {
    if (e.keyCode == 13) func.apply(this, [e]);    

  return this; 
Source: http://stackoverflow.com/a/6524616

Second and last step, we'll add the trigger to our input text field and instead of using click() or trigger('click'), we'll use mousedown().
$('input#form-text-field').onEnter(function() {

That's it. Your form should be submitting when hitting enter from within the text field.

The explanation about the why can be found on the Ajax Framework page on Drupal.org. Here's a quote:
Source: https://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax/7

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.