Greg's Blog

helping me remember what I figure out

JavaScript: Trapping the Enter Key Event

| Comments

Here is a refined script for trapping the user pressing the enter key. In certain cases under Netscape, where you have two text input fields, hitting the enter key will result in nothing happening. To this end I searched the web and found the following script: [js] [/js] This script first looks up the version of the browser you are using and if the user uses NS whichCode is equal to the event e.which. Since IE handles it differently you need to the else if statement and we set which Code for IE equal to the event e.keyCode. In both cases this will return a value. Now hitting the enter key returns the value 13. So in the next step we check for that value and if this is the case then we submit the form. This function is called by including the event handler onKeyPress next to your input fields. Hence you could call the code() function first to check whether return was hit or not, then maybe run a validation script and if successful submit the form. I would also like to point out that you will need to provide a action property to your form for Netscape to submit the form successfully. Not doing so will result in the page just sitting there. Short and sweet, but useful.

Comments