Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

Enable submit via Enter key on Ajax forms

Last updated on
September 8, 2016 - 14:07

See first comment for alternative method.

Users often submit forms by pressing the Enter key while the cursor is positioned in a text field. This is a convenient feature to quickly submit a form from the place of last edit (especially on forms that are very long) instead of having to scroll down to the Save button, position the mouse over it and click. An issue I ran into with Drupal 7 is that this is no longer working in node forms, because of the extensive use of Ajax. The Enter key is pressed after editing a text field and nothing happens. After searching quite a bit, I found a solution:

add the following function to template.php, clear cache and then test it out:

function YOURTHEME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'YOURFORMID') {
      $form['YOURINPUTNAME']['#attributes']['onkeypress'][]='if(event.keyCode==13){this.form.submit();}';
  }
}

This can be enabled for multiple inputs. As an example, if you are using the Garland theme and want to add the abitilty to submit the node form by pressing Enter in both the "Title field" and the "Authored by" field for a content type "Page" the code would look like this:

function garland_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'page_node_form') {
      $form['title']['#attributes']['onkeypress'][]='if(event.keyCode==13){this.form.submit();}';
      $form['name']['#attributes']['onkeypress'][]='if(event.keyCode==13){this.form.submit();}';
  }
}

If someone knows a better way of doing this, please add it. Also, if there is a way to generate attributes for all inputs of a certain class in a form (i.e. all inputs with class "form-text") it would be helpful to list that.

The only issue I have come accross is that pressing Enter key to submit with this technique is not properly registered with the Content locking (anti-concurrent editing) optional module. You will receive a warning of you have "Use javascript to detect leaving the node form" enabled with that module.

Happy efficient form submitting :)