like-a-trainee
like-a-trainee

Reputation: 561

Get index of currently focused input text field

How do I get the index of the current input text field I'm currently in? I'm trying to jump to the next input field after pressing enter. This is what I've got:

 var index = $('input:text').index(this); 
 $('input:text')[nextIndex].focus();

But it's not going to the next one..

Upvotes: 1

Views: 2864

Answers (1)

VisioN
VisioN

Reputation: 145458

The following code should work fine:

$("input").keypress(function(e) {
    if (e.which == 13) {
        var index = $("input[type='text']").index(this);
        $("input[type='text']").eq(index + 1).focus();
        e.preventDefault();
    }
});​

DEMO: http://jsfiddle.net/uJsMQ/

Upvotes: 1

Related Questions