0

Firstly hey all,

I'm trying to write a simple focus next script. here's my javascript code

var order = new function () {
    this.fields = [
        "#OrderViewModel_OrderCreditCartNumbers1",
        "#OrderViewModel_OrderCreditCartNumbers2",
        "#OrderViewModel_OrderCreditCartNumbers3",
        "#OrderViewModel_OrderCreditCartNumbers4",
        "#OrderViewModel_OrderCreditCartExpireMonthId",
        "#OrderViewModel_OrderCreditCartExpireYearId",
        "#CreditCardCustomerName",
        "#OrderViewModel_OrderCreditCartCvc"
    ]
    this.events = function () {
        this.followcardnumber();
    }
    this.followcardnumber = function () {
        for (var i = 0; i < 4; i++) {
            var elem = $(this.fields[i]);
            var nextelem = $(order.fields[i + 1]);
            elem.on("keyup", function () {
                if ($(this).val().length == 4 && i != 3) {
                    nextelem[0].focus();
                }
            });
        }
    }
}

$(document).ready(function () {
    order.events();
});

This code should track keyup event for first 4 selector and focus next one. The problem is when a field full script focus field[4] and it should focus next field.

any tips?

Thanks for any help or idea.

  • can you try completing sentence after The problem is when a field full script focus field[4] – Satya Sep 23 '14 at 14:57
  • @epascarello It would be helpful to explain *how* the linked topic answers this question rather than just marking as a dupe. On its face the question doesn't even seem related. It may be "obvious" to you how the answers there address the problem here, but since it's not at all the same question, that really needs to be explained. Otherwise, I think this should be reopened. – Adi Inbar Sep 23 '14 at 15:29
  • The answer explains it. Take the content of the for loop. Put it into a function, call the function with the value of `i`. Value of `i` will be what OP expects and not the length of the array +1. Personally I would not code this like OP did, there are better ways. – epascarello Sep 23 '14 at 15:33

0 Answers0