I have a small canonical lottery search form comprising of 6 input fields. Currently when the user backspaces once, it will remove the value and move to the previous input field.
I need to split the above process into two steps. Therefore one backspace removes the value, the second backspace moves to previous input field and repeat.
How can I achieve this by adding to my current jquery if/else statement?
$(function() {
$("#target input").keyup(function(event) {
if ($(this).val().length == 1) {
$(this).nextAll('input').first().focus();
} else if ($(this).val().length > 1) {
var new_val = $(this).val().substring(1, 2);
$(this).val(new_val);
$(this).nextAll('input').first().focus();
} else if (event.keyCode == 8) {
if ($(this).prev('input')) {
$(this).prev('input').focus();
}
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<form id="target">
<input type="number" id="keyword_1" maxlength="1" min="0" max="9" style="width: 35px !important; etter-spacing: 1.5px;">
<input type="number" id="keyword_2" maxlength="1" min="0" max="9" name="keyword_2" style="width: 35px !important; etter-spacing: 1.5px;">
<input type="number" id="keyword_3" maxlength="1" min="0" max="9" name="keyword_3" style="width: 35px !important; etter-spacing: 1.5px;">
<input type="number" id="keyword_4" maxlength="1" min="0" max="9" name="keyword_4" style="width: 35px !important; etter-spacing: 1.5px;">
<input type="number" id="keyword_5" maxlength="1" min="0" max="9" name="keyword_5" style="width: 35px !important; etter-spacing: 1.5px;">
<input type="number" id="keyword_6" maxlength="1" min="0" max="9" name="keyword_6" style="width: 35px !important; etter-spacing: 1.5px;">
<button class="btn btn-info" id="search-ticket" type="submit">Search</button>
</form>