Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
    }
});
like image 644
msmafra Avatar asked Apr 29 '11 17:04

msmafra


1 Answers

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();
                }
            }
        });     
like image 146
Tom Walters Avatar answered Sep 24 '22 21:09

Tom Walters