I want to retain the conventional 'form submits when i press Enter' behavior because users are familiar with. But by reflex, they often hit enter when they finish with a text input box - but before they are actually done with the complete form.
I'd like to hijack the Enter key only when then focus is on a certain class of input.
Looking Related Questions this looks like what I'm looking for:
if (document.addEventListener) {
document.getElementById('strip').addEventListener('keypress',HandleKeyPress,false);
} else {
document.getElementById('strip').onkeypress = HandleKeyPress;
}
but the if (document.addEventListener) {
part is unfamiliar to me.
You can capture and cancel the enter
keypress
on those fields like this:Then on your inputs just give them a
class="noEnterSubmit"
:)Looking ahead in case others find this later, in jQuery 1.4.3 (not out yet) you can shorten it to this:
Just add following code in
<Head>
Tag in your HTML Code. It will Form submission on Enter Key For all fields on formTo allow enter only on a specific class (in this example, 'enterSubmit'):