Submit a Form using Enter Key

Here is a little Javascript I picked up. I run into a problem in IE wherein a form won’t submit when you hit the Enter key it just reloads itself but clicking the Submit button would processes your form. The form works with the Enter key fine in FF but not in IE.

Chances are that you maybe calling a function on your Submit button that will load the requested page? Something like this?

input type=”submit” name=”search” value=”Search” onClick=”myfunction();” >

Where in the myfunction() function can be anything like validating a page before it submits your data, or trigger another event or load an ajax request.

To fix this and allow using the Enter key to submit your form in IE. Create a new function in your Javascript like below which checks for the Enter key being pressed, keyCode == 13 then call your original event myfunction.

function checkEnter(e){
var characterCode
if(e && e.which){
e = e
characterCode = e.which
else {
e = event
characterCode = e.keyCode

if(characterCode == 13){
myfunction(); // onClick function you were loading on the submit button
return false
return true
} // checkEnter

Then on one of the input fields call the onKeyPress event and call your new function.

input type=”text” name=”keywords” onKeyPress=”return checkEnter(event)” >

Leave a Reply

Your email address will not be published. Required fields are marked *