Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


form elements

Submit form when hitting return by simonchristieis
Posted: 3 Feb 04

A few people have asked for this functionality recently, so I thought I'd write a FAQ.

first of all we need to tell the page to detect when a key is pressed.

window.onkeypress = enterSubmit;

function enterSubmit(){
if (event.keyCode==13)

hey thats great!, but hang on - what if you have form validation on the page?

Using submit() will bypass any onsubmit="return ... statements you have in the form, however you could adapt the function to account for this (assuming formValidation() is your function name):

window.onkeypress = enterSubmit;

function enterSubmit(){
if (event.keyCode==13){
if (formValidation())

Now we're getting near to some decent functionality, but as you can see the form being submitted is the first form on the page.

How do you overcome this?

Within each form opening tag you could include:

<form name="myForm" onFocus="javascript:document.strForm=this.name">

and change your javascript to:

// if no other form has been focussed, submit first form
// on page
document.strForm = 0

window.onkeypress = enterSubmit;

function enterSubmit(){
if (formValidation()){
if (event.keyCode==13)

Good Luck.


Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close