I am running into inconsistent behavior between Chrome and Internet Explorer, where IE functions and Chrome doesn't. I am trying to make a form accept the Enter key to be submitted. This is the Javascript for it:
$("#formElementId").on("keyup", function (e) {
if (e.keyCode == 13 || e.which == 13) {
if (e.target.id == "excludedElementId")
e.stopPropagation();
else
document.getElementById("submitButtonId").click();
}
});
The reason it doesn't work in Chrome is because a form
element doesn't accept key input. This can be fixed by assigning the form
element a tabindex
value, which will allow it to be focusable. This is the recommended solution in this SO post.
This doesn't seem like the right solution. When focused, a soft border displays around the element, which I will have to hide. Also, its tabindex it will precede the actual form input controls in tabindex, which is not desired.
Is there a better way to handle the enter key for Chrome? And lastly, why does IE accept key input at the form level?