Submit Form on Enter Key

uh... did you know that you can't submit a form by pressing Enter when having more then 1 input field?
So let's say you have a simple form like this:

HTML:
  1. <form action="" method="post">
  2. <input type="text" />
  3. </form>

Now when the input text has the focus and you press Enter this form will submit. But once you have more than just one input field, it will not.

HTML:
  1. <form action="" method="post">
  2. <input type="text" name="a" />
  3. <input type="text" name="b" />
  4. </form>

I solved this by adding an invisible submit item.

HTML:
  1. <input type="submit" style="width:0px;height:0px;border:none">

Crazy, but I did not know that.

15 Comments

  1. BlueSky 2007-10-13, 8:28 pm

    Wow, a simple, sharp solution.

    Liked it.

  2. Alf Stockton 2007-12-9, 11:39 am

    I do not know how many times to say it but THANK YOU, THANK YOU,THANK YOU, THANK YOU.
    I have been looking for this for quite some time.

  3. Paradox 2008-01-16, 12:09 am

    A simpler way to do this would be to use:

  4. Paradox 2008-01-16, 12:12 am

    A simpler way to do this would be to use:

    [code][/code]

  5. Paradox 2008-01-16, 12:15 am

    Never mind, instead of using: style=”width:0px;height:0px;border:none” you can use style=”display:none”

  6. Form not submitting on enter key with multiple form fields : Heck Of A World 2008-02-5, 3:17 am

    [...] So this is one of those really stupid things that was virtually impossible to find on google. One permutation was the golden child and brought me to this wonderful blog post. [...]

  7. Grzegorz 2008-05-27, 10:28 pm

    It seems, that if you write:
    style=”display:none”
    instead of:
    style=”width:0px;height:0px;border:none”,
    our favourite IE will ignore the input tag and enter key will not work.

  8. Jonny 2008-06-17, 10:29 pm

    So then try “visiblity: hidden”, although that will leave an ugly block of whitespace in the way.

  9. Jonny 2008-06-17, 10:39 pm

    Nope, just tried it, doesn’t work in IE 7

  10. Jonny 2008-06-17, 10:44 pm

    This worked best in Safari, Opera, Firefox and IE:

    style=”width: 0px; height: 0px; position: absolute; left: -50px; top: -50px;”

    Absolute positioning became necessary because the button was still visible in Firefox and Safari and in particular Safari completely crashed when clicking the button having an empty value attribute.

  11. James 2008-07-31, 10:37 am

    Thanks, been wondering about this for a while and finally got round to fixing it.

  12. technimad 2008-08-5, 2:51 pm

    Cool, thanks for sharing, this works really well for accessible websites. Create a funky submit button which does a javascript submit, and hide the normal submitbutton with this technique!

    I’ve been using the style
    [code]width: 0px; height: 0px; position: absolute; left: -500px; top: -500px;[\code]
    to move the submitbutton completely offscreen (Safari still showed a dot). Works great in IE6 aswell.

  13. Vali 2008-09-13, 7:19 pm

    The border was still visible in Opera, so I did this:

    <input type=’submit’ value=’Login’ style=’width:0px; height: 0px; border: none; padding: 0px; font-size: 0px’>

    Tested in IE6/7, Opera 9, Firefox 3

  14. Roger 2008-09-17, 4:14 pm

    This is a customer site that we’re building and on the login page I thought I was going to use a hidden submit button. The correct CSS code to implement this is “display:none”. Other code above gives hidden wierdness in margins, etc. from browser to browser.

  15. Alex 2008-09-24, 12:43 am

    Strangely enough, my problem was that with only ONE password field and no other text fields, if I had a button, pressing ENTER didn’t work!

    So, inspired by your method, I added a second hidden text field before my password field and hey it worked!

    Thanks a lot for this!

Add a Comment