/ Published in: JavaScript
URL: http://davidwalsh.name/prevent-early-form-submission-hijacking-enter-key
A customer asked me to create a timecard form a few years ago. Everything with the form worked great but users would mistakenly press the enter key before their form was completed. They asked that I make the enter key move the cursor to the next input instead of submitting the form. This is how I'd do that using MooTools JavaScript. The MooTools JavaScript:
Expand |
Embed | Plain Text
var inputs = $$('input.hijack'); $each(inputs,function(el,i) { el.addEvent('keypress',function(e) { if(e.key == 'enter') { e.stop(); if(inputs[i+1]) { inputs[i+1].focus(); } //last one? if(i == inputs.length-1) { $('submit-button').focus(); } } }); });
You need to login to post a comment.
