Do you think that any form on a web-page may be submitted by pressing “Enter”?
I was surprised to discover, that this fact is true only for the forms having a submit button or the single input field. It appears that the form more than one input but without “submit” won’t submit if you press the “Enter” key.
You are welcome to check: the first form submits (and alert a message), while the second one does nothing.
After a quick search this problem appears to be a known and rather old one. People on the web say this bug is known since Netscape times; also they say that there is an open bug on it for IE – I cannot check, as Microsoft does not allow unregistered users to see its bugs.
Where does this issue come from?
This is what we see in W3C HTML specifications:
“When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.”
Does it mean that otherwise UA should not accept it? Obviously, our browsers think so. I’ve checked in all my browser zoo (all the latest browsers on Linux and Windows (XP and 7), plus IE7, IE6, and Safari on Mac) – the behavior is equal in all of them except Safari, which nicely submits both of the forms.
So, though we most likely won’t use the input of a type button, we still must add it, being hidden, to any form with more than one input field:
display:none it works in Firefox and Opera, but won’t work in Chrome, so I stopped checking. The example above has
visibility:hidden on the button; it is ok in all the browsers I have except IE8 and below (surprising, indeed). Setting width and height to zero leaves us with ugly rectangle; setting a JS event to catch “Enter” is bad for all users who has their JS off. I think, from here any site has to make its own decision. Or maybe someone knows the ultimate cross-platform solution?