Forum Moderators: open
I too have run into this problem on many occasions and have never quite understood, or indeed discovered, the rules that seem to be at play with regards to submit on enter.
· It always seems to work if you have a true submit input type within the same form tags.
· It also seems to work (without a submit input) if you have only one text field in the form (including passwords).
However, I did find a workaround/hack for IE5+.
Within your form tag include a submit button but set the height and width to 0px in it's style attribute, eg:
<input type="submit" value="" style="height:0px;width:0px" tabindex="999" hidefocus="true" />
The tabindex and hidefocus remove the button from the tab order and prevent the visitor focussing on something they can't see. This is quite important as, if they're likely to be using enter to submit the form there's an equally good chance they'll want to navigate the form with the tab button.
This doesn't work in NN4, NN6, or Opera, only IE5 as far as I know. If anybody has a cross-browser solution to this problem (short of listening for the enter event) or knows the rules of submit on enter I'd be delighted to hear them!
document.body.onkeypress = enterKey
and then add a new function:
function enterKey(evt) {
var evt = (evt) ? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode == 13) {
validateForm()
}
}
This function calls my validateForm function when the Enter key is pressed (validateForm just makes sure both fields have something in and then submits the form). I've tested it in IE 5.5 and N 6, and according to the Good Book it should work in IE 4+ and NN 4+ (but my client doesn't need those browsers so I haven't bothered testing them).