I generate inputs (with a max length of 1) dynamically using JavaScript so I don't have to type them all in manually. I made them such that when I type into them, they go to the next input and when I press backspace, they delete the value inside the input, then go to the previous one.
Expected behaviour
When I press backspace, it'll delete the letter, then go to the previous input.
What actually happens
When I press backspace, it'll delete the letter, then do this weird shuffle, and then I press backspace again, and then it moves to the previous input.
Here is a JSFiddle that reproduces the issue I'm having.
Since Stack Overflow won't let me post this without code (here is the jQuery code):
function generateInputRows() {
var inputHTML = "";
for (var i = 0; i < 5; i++) {
inputHTML += '<div class="inputColumn"><input maxlength="1" type="text"><div>';
inputHTML += (i + 1);
inputHTML += '</div></div>';
}
inputHTML += '</div>';
return inputHTML;
}
function setUpTyping(){
$('.inputColumn input').on('input',function(event){
var inputs = $('.inputColumn input');
var index = inputs.index(this);
inputs.eq(index + 1).focus();
});
$('.inputColumn input').on('keyup', function(e) {
if(e.keyCode == 8 && $(this).val() == ""){
var inputs = $('.inputColumn input');
var index = inputs.index(this);
inputs.eq(index - 1).focus();
}
});
}
$('#rows').html(generateInputRows());
setUpTyping();