2

I have this code:

function THEME_form_user_login_alter (&$form, &$form_state){
  $form['actions']['submit']['#ajax'] = array(       
        'callback' => 'ajax_process_form',
        'wrapper' => 'user-login',
        'effect' => 'fade',
        'speed' => 'fast',
        'method' => 'replace'
    );  

};

It work when i click on the submit button, but when i press ENTER Ajax doesn't get triggered.. Is there a way around that?

Edit: I tried to enable ajax on all fields and set keystroke to True, but then only one field is sent so i get an error as the form process script is expecting two fields.

  • I actually had to remake the AJAX function manually with some good old jQuery, and then I finally had a normal behavior. – silkAdmin Jun 25 '11 at 9:04
5

I realize this is an old thread but it shows up high in the Google search results.

Just set your submit button's Ajax event to 'click'. I believe its default value is actually 'mousedown' which won't make it trigger when the enter key is pressed within a field of the form.

If you set it to 'click', that event will trigger when the submit button is clicked as well as when the enter button is pressed within a form field.

2

Just make sure you explicitly set ajax event to click and it'll work:

$form['actions']['submit']['#ajax'] = array(       
    'callback' => 'ajax_process_form',
    'wrapper' => 'user-login',
    'effect' => 'fade',
    'speed' => 'fast',
    'method' => 'replace',
    // This is what you need:
    'event' => 'click'
);  
1

I haven't tried this, but you should be able to set the event on ajax to include keydown:

function THEME_form_user_login_alter (&$form, &$form_state){
  $form['actions']['submit']['#ajax'] = array(       
    'callback' => 'ajax_process_form',
    'wrapper' => 'user-login',
    'effect' => 'fade',
    'speed' => 'fast',
    'method' => 'replace'
    'event' => array('click', 'keydown'),
  );
}

I'm not sure how well keydown will work on a submit button, usually you want something more complex that makes sure that the matched key is Enter. Alternatively you could catch the Enter submit yourself and prevent default (form submit) and the click the button yourself. That would trigger the ajax call like it was clicked and would keep the Drupal powered AJAX intact, which makes things easier to handle server side.

  • 1
    Hi Googletorp, the keydown is useful only when the focus is on the element, in that case the submit button, so unless the user tabs out of the fields this is a pretty unlikely scenario. (i also tried adding a keydown on field, it was very odd..) I think like you said catching the Enter Keydown manually seem to be the only way to go. – silkAdmin Jun 27 '11 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.