Demo for How Forms Submit When Pressing Enter
Test Page - Form Submitting Using Enter
Here is the problem. You have a form with with 'submit' event listener, and you have a button inside that form with a 'click' event listener. Now, as users are completing fields in the form, you want them to be able to hit 'enter' to trigger the 'submit' event callback. But you do not want to trigger the 'click' event on the element you are using as buttons. Nor do you want to allow the forms default submittal behavior, because you want to capture it and send an AJAX request.
This page looks at 6 different types of form/button models and how different browsers react. The first half of the page is dedicated to my testing code that I used to evaluate different browser. The second half of the page shows my conclusions.
If you are running the tests yourself, they print to the browser's console log, so look there to see the results.
Test # / Browser | 1 Singlea | 2 Single | 3 Single | 4 Single | 5 Single | 6 Single |
---|---|---|---|---|---|---|
FireFox 3.5 & Opera 9.6 (Enterb) | bothc | both | both | both | submit | submit |
FireFox 3.5 & Opera 9.6 (Click) | both | both | both | both | click | click |
Chrome 2 & Safari 4 (Enter) | both | both | submit | submit | submit | submit |
Chrome 2 & Safari 4(Click) | both | both | both | both | click | click |
Chrome 18 (Enter) | both | both | both | both | click | click |
Chrome 18 (Click) | both | both | both | both | click | click |
IE 6 & 7 (Enter) | submitd | submitd | submit | submit | submit | submit |
IE 6 & 7 (Click) | both | both | click | both | click | click |
Test # / Browser | 1 Doublea | 2 Double | 3 Double | 4 Double | 5 Double | 6 Double |
---|---|---|---|---|---|---|
FireFox 3.5 & Opera 9.6 (Enterb) | both | both | both | both | none | none |
FireFox 3.5 & Opera 9.6 (Click) | both | both | both | both | click | click |
Chrome 2 & Safari 4 (Enter) | both | both | submit | submit | submit | submit |
Chrome 2 & Safari 4(Click) | both | both | both | both | click | click |
Chrome 18 (Enter) | both | both | both | both | none | none |
Chrome 18 (Click) | both | both | both | both | click | click |
IE 6 & 7 (Enter) | bothd | bothd | none | bothd | none | none |
IE 6 & 7 (Click) | both | both | click | both | click | click |
a Single and Double refer to the number of fields in the form (1 or 2 fields were used for testing purposes)
b Two rows are used for each browser, "(Enter)" for hitting the enter key in the field and "(Click)" for clicking the button
c 'both' means that the form submitted and the `button` click fired
d IE can required 2 enters if the field is empty before firing the submit