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.

10 Comments

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

    Gravatar

    Wow, a simple, sharp solution.

    Liked it.

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

    Gravatar

    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

    Gravatar

    A simpler way to do this would be to use:

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

    Gravatar

    A simpler way to do this would be to use:

    [code][/code]

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

    Gravatar

    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

    Gravatar

    [...] 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

    Gravatar

    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

    Gravatar

    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

    Gravatar

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

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

    Gravatar

    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.

Add a Comment