I’ve found several jQuery syntaxes for nullifying the enter on a form.
First one:
$("form input[@type=text]").bind("keypress", function(e) {
var code=e.charCode || e.keyCode;
return (code==13)?false:true;
});
Second one:
$("form").bind("keypress", function(e) {
if (e.keyCode == 13) return false;
});
My version:
$('form').keypress(function(e) {
if (e.which == 13) return false;
});
My question is:
Q: Is my version ok to use, or is there a best practice for defeating the enter key from doing a form submit?
I think your method is probably fine. Capturing the form’s submit event won’t really help for your situation, because there’s no way (that I can think of at least) to know how the form was submitted.
The only suggestion I would make is to change
return false;toe.preventDefault();