Last updated January 14, 2012. Created by Coupon Code Swap on January 13, 2012.
Log in to edit this page.

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 :)

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I found another, perhaps better way to accomplish this. If you change the weight of the actions buttons so that they are above any Ajax items in the node form, pressing Enter to save the form works as expected. The other nice thing about this method is that it does not interfere with the Content locking module :)

Here's how:

function YOURTHEME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'YOURFORMID') {
      $form['actions']['#weight']=-1;
  }
}

Adjust the weight as necassary. It would be nice if there is a way to print the actions buttons at both the top and bottom of form...

$form['actions']['submit']['#ajax'] = array(
'callback' => 'login_form_handler',
'wrapper' => 'errors',
'effect' => 'fade',
'event' => 'click'
);

just add 'event' => 'click' you your submit ajax element in the form.

Really simple, thanks!

Shabana Navas
Drupal Developer
Blackborder

Awesome tip, thanks!

enter = click on the Ajax works beautifully. Thanks!

You save my life!