Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Submit The Form When The User Presses Enter

Different browsers have different default behaviors about what to do when the user hits enter in a form. MSIE almost always submits the form, while Netscape will often just beep at you. Although it's usually best to leave the default browser behavior as it is, for some forms people just naturally tend to hit "enter" when they are ready. This is particularly true for login forms. With a little JavaScript we can set the form to submit on enter.

First, copy this script exactly as-is into the <HEAD> section of your document:

<SCRIPT TYPE="text/javascript">
<!--
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}
//-->
</SCRIPT>

For each field which should submit the form when they hit enter add an onKeyPress attribute like this:

<FORM ACTION="../cgi-bin/mycgi.pl">
name:     <INPUT NAME=realname SIZE=15><BR>
password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
           onKeyPress="return submitenter(this,event)"><BR>
<INPUT TYPE=SUBMIT VALUE="Log In">
</FORM>

Add the attribute like it is in the example. No modification is needed. This gives us this form:

name:
password:

 
 
  Copyright 1997-2002 Idocs inc. Published under the terms of the Open Content License Design by Interspire