1

I have 3 inputs that need to tab to the next when the max length are reached. These 3 inputs come with values and when the user change the value of the first input it focus on the next and so on.

My problem is that since my second input has already the length it jumps to the third input. If the use input the values slowly it don't do this.

The source of the problem is that if the typing is too fast the first keyup event is fired after the second type and it fires on the second input.

I've written a jsfiddle with the problem and this is my function to wire the auto focus change.

 function WireAutoTab(CurrentElement, NextElement) {
    CurrentElement.keyup(function (e) {
        //Retrieve which key was pressed.
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
        var FieldLength = CurrentElement.attr('maxlength');

        //If the user has filled the textbox to the given length and
        //the user just pressed a number or letter, then move the
        //cursor to the next element in the tab sequence.   
        if (CurrentElement.val().length >= FieldLength && ((KeyID >= 48 && KeyID <= 90) || (KeyID >= 96 && KeyID <= 105)))
            NextElement.focus();
    });
}

Is there any other event that I can use to prevent this? The behavior that I want is that even if the second input has value, it stops on it.

Luiz
  • 542
  • 5
  • 10
  • (unrelated to your question, but) I think your `WireAutoTab` function should be inside your `$.fn.autoTab` closure. That way it isn't leaked globally. Just a suggestion :) – Ian Apr 05 '13 at 19:58
  • Well what do you want to happen when, in your example, you type 2 characters in the first textbox, get autofocused to the second, and type? – Ian Apr 05 '13 at 20:00
  • Ian, I know that it's global, this is just an example code not the real one. – Luiz Apr 05 '13 at 23:43
  • What I want is that when the first input is typed, the second receive the focus. The code works, but if the second has a value and the typing is too fast the second keyup event is fired on the second input and the third receive the focus. – Luiz Apr 05 '13 at 23:46
  • Yes I understand that, but I'm asking what you **want** to happen in that scenario? – Ian Apr 06 '13 at 00:23
  • The correct is that it stops on the second input. – Luiz Apr 06 '13 at 12:13
  • **BUMP** Any ideas from OP or community? – semuzaboi Sep 17 '14 at 08:56

0 Answers0