jQuery how to make Enter (Return) act as Tab key through input text fields but in the end trigger the submit button

msmafra picture msmafra · Apr 29, 2011 · Viewed 9.8k times · Source

I've blocked Enter (return) key, actually, transformed it in Tab key. So when pressed inside input text fields it acts as Tab key. This is good, but I need it to trigger the submit button when pressed in the last field, below is the code for the Enter key mutation:

            $('input').keydown(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    $(this).nextAll('input:first').focus();
                }
            });

And the form code is a sequence of input type="text" and a button type="submit" at the end [Edited] Actually the code is this one, taken from jquery: Enter to Tab trigger in specific parts. But I don't know why it was working yesterday today is not working:

$(':text').keydown(function(e) {
    if(e.keyCode == 13 && $(this).attr('type') != 'submit') {
        e.preventDefault();
        $(this).nextAll('input:first').focus();
    }
});

Answer

Tom Walters picture Tom Walters · Apr 29, 2011

If you give your last input the class of last then simply modify your code to something like

$('input').keydown(function(event) {
            if(!$(this).hasClass("last")){
                if(event.which == 13) {
                    event.preventDefault();
                    $(this).nextAll('input:first').focus();
                }
            }
        });