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