Spartan Java

Enabling “Enter” in a form without a submit button

by ricardoz on Oct.13, 2009, under JavaScript, Tips

I find it quite annoying when I can’t post/confirm a web form using the Enter key.

Here is a nifty little javascript trick to do it when you do not need/want a submit input in your html.
First include this javascript function somewhere accesible for your HTML:

1
2
3
4
5
6
7
8
9
10
11
12
function submitenter(e, action) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;
 
  if (keycode == 13) {
    eval(action);
    return false;
  } else
    return true;
}

And then add the following to each form field where u want the Enter key to work:

onKeyPress="return submitenter(event, 'doSomething();')"

The function doSomething() (which you must write off course) will be executed everytime the users presses Enter in one of the form fields.

Bookmark it / share it:

  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • Technorati
  • StumbleUpon
  • email
  • Facebook
  • LinkedIn
  • Print
  • NewsVine
  • Twitter
:, , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...