Hey Folks,
During your web development odyssey, you might have encountered a situation when you need to perform certain action without the “Submit button”. Or you want as soon as user completed the form and hits Enter the form gets submitted. Well this is true when you have submit button embeded on the form. However there are situations when you do not have form or a submit button. So in that case how would you submit the form or perform certain action.
Generally users tend to hit “Enter” key when they complete their details on a form. A very trivial example is when you input email address and password , you certainly hit “Enter” to sign into your mailbox. So to give the same kind of experience without asking user to click on any of the buton, you can use the following code.
<form method="post" > <input type="text" size="20" name="test_txt" onkeypress="if(event.keyCode==13) {this.submit();}" /> <input type="button" value="I can also submit" /> </form>
In the above code we have associated a function with the event “onkeypress”. The listener is waiting for the Key press whose keycode is 13. Enter has the keycode 13, so as soon as user hits 13 the Javascript statement “this.submit();” submits the form. It is worth mentioning that instead of submit statement you can also write your own Javascript function name there. So when user hits the Enter key your function is called and then whatever the logic is written in the function it executes.
Hope this helps you in someway.
Stay Digified !!
Sachin Khosla






I’m no expert but sometimes in my forms I have submit buttons that do different things. e.g., one form that handles submitting search criteria, and limit criteria with one submit button for each to differentiate.
In this situation, pressing enter seems to jump to the first submit button it finds and executes that one.
In this case, your javascript code fails (tested in firefox on mac) to override this behavior.
A little bit of plucking later, I discovered that if you place a hidden div or span right after the text field you expect them to hit enter in, and place code for a submit button in it, that submit button is executed when enter is pressed.
Hope this helps someone.
Complete fail. This doesn’t work with basic W3C browsers including Firefox. Before you go to the trouble to write this garbage and waste all of our time, how about testing it first!