Javascript – Submitting a form by hitting “Enter”

Programming,Snippets — Jeff Eske on September 6, 2013 at 9:41 am

I’m working within a commercial web-based application that uses a lot of forms, none of which can be submitted by simply hitting the Enter key.  Users were wanting to be able to at least submit a particular search form by simply hitting Enter, rather than having to click the “Search” button, so I decided to do some checking.  I found a pretty simple javascript function that can handle what I need done.  Basically, it watches key presses and when it sees the Enter key pressed (keyCode 13) it runs my search function (some_function_here).  The nice thing is that the default Search button is still available and works the same as always.

Simply add this code to the HEAD code on your page, and change the actions within the if statement to match what the current button does “onClick” and you should be good-to-go.


<script language=”Javascript”>
function detectEnterKey(e) {
var evt = e || window.event;
if(evt.keyCode == 13){
return document.defaultAction;



Jeff Eske

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Jeff's Meanderings*