Thanks for this.
I took taht, but I had some problems with it when coming back on an full input through keyboard navigation.
First, I tried to filter Shift and Tab keys, but I still had a problem with quick key strokes : when releasing quickly two keystrokes, the first keyup move to next input field and the second keyup is processed on the input field I just came back to. If the field is also full, then I just pass to next input (while the user may want to modify it).
The solution I use consists of moving to next input if the keyup is processed after a key has been pressed on the input field.
I've also seperated the logic of listeners, testing moving conditions (in this case : enter and field complete) and moving to next input.
var setChangeInputWhenComplete = function() {
document.getElementsByTagName("input").forEach(elt => {
// Variable to trace if a key has been pressed on input since arriving on it
var keyPressed = false;
// Records if a key has been pressed on input field in order not to pass to next field if the keyup event occurs on a field we've juste arrived on
elt.addEventListener("keypress", function(event) {
keyPressed = true;
});
// Clears previous keypressed recording when we come back on input field
elt.addEventListener("focus", function(event) {
keyPressed = false;
});
elt.addEventListener("keyup", function(event) {
// When quickly releasing a second key, pressed on previous field, when arriving on a new field, we should not pass to next field even if the field is already full
if (keyPressed) {
// If field is full, pass to next input
if (mustMoveToNextInput(event)) {
moveToNextInput(event);
}
}
});
});
};
var moveToNextInput = function(event) {
//set variable for next
var next = event.srcElement;
//loop for the next element of the inputField
while (next = next.nextElementSibling) {
//conditional if the next is not present, so last element we break the code
if (next == null)
break;
//conditional to for the next element that is actually an input tag
if (next.tagName.toLowerCase() == "input") {
next.focus();
break;
}
}
};
var mustMoveToNextInput = function(event) {
var inputField = event.srcElement;
var maxLength = parseInt(inputField.attributes["maxlength"].value, 10);
return (event.keyCode === 13 || inputField.value.length >= maxLength);
};
setChangeInputWhenComplete();