Pressing Enter in a Field Submits a Form Instead of Using the Submit Button Next to the Field

Mindwatering Incorporated

Author: Tripp W Black

Created: 04/02/2002 at 06:30 PM

 

Category:
Notes Developer Tips
JavaScript

The issue is most seen in IE with a form having only one field. If you press enter while in that field it submits the form instead of using the action button next the field that you were wanting the user to use.

This code fixes this isse and has the enter captured and executes the button.

This example below assumes a button named "Go" which has been given the name Go in the name field on the HTML properties tab for the button.
The javascript code below is placed in the field where the user is typing in the event called "onKeyDown".

if (window.event.keyCode == "13") {
document.forms[0].Go.click();
return false;
}


Updated function for compatibility w/Mozilla Firefox:

<input id="Text1" type="text" onkeydown="return kd(event)"/>

function kd(e)
{
var intKey = (window.Event) ? e.which : e.keyCode;
if (intKey == 13) { //enter key
document.forms[0].Go.click();
return false;
}
return true;
}

previous page