Mental Jetsam

By Peter Finch

How to submit a HTML form with the Enter Key

Posted by pcfinch on April 23, 2007

If your form does not have a SUBMIT button then, in some cases the browser will not automatically submit the form when you press ENTER. Here is how to submit the form when the user presses the ENTER key.

The code simply intercepts all the key presses on the page and when the ENTER key is pressed if submits the form by calling the appropriate JavaScript function. In this case submitForm(), however, it could be any other function. This function works on Mozilla and Internet Explorer (IE).

document.onkeypress = processKey;

function processKey(e)
{
  if (null == e)
    e = window.event ;
  if (e.keyCode == 13)  {
    submitForm() ;
  }
}
Advertisements

8 Responses to “How to submit a HTML form with the Enter Key”

  1. fotinorod said

    Hello

    I am Lucy, I have found your website while searching for some info at Google. Your site has helped me in a big way.

    G’night

  2. aravind said

    Great solution!!!!!!

    Really was very helpful for me…

    saved a lot of time..

    Thks

  3. Ranjit Gundu said

    Simple and easy solution…thank you.

  4. this works!!!

    thanks for this man.

    guys, just remember to change

    submitForm() ;

    to whatever you have in your onClick=”yourvaluehere()”

    otherwise it won’t work.

  5. Northie said

    An alternative solution is to place an “invisible” submit button in your form:

    <input type=”submit” style=”height:0px; width:0px; border:0px;”>

  6. Dave said

    Hi pcfinch,

    Great work! Do you have a solution for Opera and Chrome browsers. It does not work on them…

  7. CC said

    Thank you! your solution is just simple and great!

  8. Pasha said

    Thank you for this solution!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: