I made an OTP design. So I have 6 inputs and everytime the user types a number it will go automatically to the next input. I have made the code below but the .select() is not working on mobile phones. Can someone help me with this?
Codepen: https://codepen.io/aceraven777/pen/wvmbadQ
HTML
<div class="otp-group">
<div class="otp-digit otp-digit-1">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
<div class="otp-digit otp-digit-2">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
<div class="otp-digit otp-digit-3">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
<div class="otp-digit otp-digit-4">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
<div class="otp-digit otp-digit-5">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
<div class="otp-digit otp-digit-6">
<input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
</div>
</div>
JS
$('.otp-digit').each(function() {
var $otp_digit = $(this);
var $input = $otp_digit.find('input');
$input.attr('maxlength', 1);
$input.attr('pattern', '\\d*');
$input.on('keyup', function(e) {
var $input = $(this);
var $otp_digit = $input.closest('.otp-digit');
var $prev_otp_digit = $otp_digit.prev();
var $next_otp_digit = $otp_digit.next();
// Backspace or left arrow key
if (e.keyCode === 8 || e.keyCode === 37) {
if ($prev_otp_digit.length && $prev_otp_digit.hasClass('otp-digit')) {
$prev_otp_digit.find('input').select().focus();
}
}
// Right arrow and numbers
else if (e.keyCode === 39 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
if ($next_otp_digit.length && $next_otp_digit.hasClass('otp-digit')) {
$next_otp_digit.find('input').select().focus();
}
}
// Backspace
if (e.keyCode === 8) {
$input.val('');
}
else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
var characterCode = ((96 <= e.keyCode && e.keyCode <= 105)? e.keyCode - 48 : e.keyCode);
$input.val(String.fromCharCode(characterCode));
} else {
return false;
}
});
$input.on('keypress', function(e) {
var keyCode = e.keyCode || e.which;
// Allow only typing numbers
if (keyCode >= 48 && keyCode <= 57) {
return true;
}
return false;
});
$input.on("change",function(e) {
var $this = $(this);
var input_value = $this.val().replace(/[^0-9]/gi, '');
$this.val(input_value);
var $form = $this.closest('.gigya-otp-login-form');
var $inputs = $form.find('.otp-digit input');
var otp_code = '';
$inputs.each(function() {
otp_code += $(this).val();
});
});
$input.on("cut copy paste",function(e) {
e.preventDefault();
});
});
If the jQuery .select() or segmenting it into multiple input isn't possible, maybe someone can give me an alternate solution.