0

I have to automatic focus next input element when a number has been entered.

My HTML:

          "<input id='num-1' class='inp-num' data-pos='0' type='text' maxlength='1' name='one' onkeypress='isInputNumber(event)' autofocus='autofocus'/>" +
          "<input id='num-2' class='inp-num' data-pos='1' type='text' maxlength='1' name='two' onkeypress='isInputNumber(event)'>" +
          "<input id='num-3' class='inp-num' data-pos='2' type='text' maxlength='1' name='three' onkeypress='isInputNumber(event)'>" +
          "<input id='num-4' class='inp-num' data-pos='3' type='text' maxlength='1' name='four' onkeypress='isInputNumber(event)'>" +

And in my javascript I have:

  $('.inp-num').on('keyup', function() {
    if ($(this).val()) {
       $(this).next().focus();
     }
  });

This actually works fine, thing is when Im trying to use it on my cellphone, it keeps staying at the same input, doens't go to the next one. I really need help on this.

Thanks!

Jesús V.
  • 129
  • 1
  • 3
  • 11

1 Answers1

0

The browser of your cellphone did not receive the value inside the input element before the keyup event is fired. Therefor if ($(this).val()) is false at this time. You need to use another event type (like input) to avoid this.

Further information and a playground:

https://www.w3schools.com/jsref/event_onchange.asp

$('.inp-num').on('input', function() {
 if ($(this).val()) {
   $(this).next().focus();
 }
});
messerbill
  • 5,499
  • 1
  • 27
  • 38