Javascript – Submitting a form by hitting “Enter”

Posted on September 6, 2013

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.

[code]

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

[/code]

 

Jeff Eske

Leave a Comment

If you would like to make a comment, please fill out the form below.

You must be logged in to post a comment.

© Copyright Jeff's Blog • Powered by Wordpress • Using Detour theme created by Brian Gardner.