I found numerous tutorials on how to perform the act of turning an enter key into a tab key. It seemed simple enough, capture the keypress event and return false while setting the focus to the next field.
So I went searching and found this link, which helped with the jQuery code needed, so I put it all together and did some testing. The code below seems to be the best option if you’re using jQuery.
$(document).ready(function(){
// get only input tags with class data-entry
textboxes = $("input.data-entry");
// now we check to see which browser is being used
if ($.browser.mozilla) {
$(textboxes).keypress (checkForEnter);
} else {
$(textboxes).keydown (checkForEnter);
}
});
function checkForEnter (event) {
if (event.keyCode == 13) {
currentBoxNumber = textboxes.index(this);
if (textboxes[currentBoxNumber + 1] != null) {
nextBox = textboxes[currentBoxNumber + 1]
nextBox.focus();
nextBox.select();
event.preventDefault();
return false;
}
}
}I hope this helps someone out looking for a nice Enter to Tab function, especially nice is the ability to limit which inputs get the enter key, and which switch focus.
**Edit**
Corrected code for missing parenthesis at end of the ready method. Thank you to Ricardo SDL for catching this error.