Keydown function jumping an input in the TAB

1

Good afternoon, I have a function that when I press the enter it creates a new series of inputs in the first line pushing the filled ones down, so that when I pressed the tab in the last also the same function occurs, however, when I press the enter, regardless of the input that it is, it creates another input string by placing the focus on the first input, exactly as programmed, perfect, already in the tab, which is to work only in the last input that in case it is already so, with the enter, it focuses on the first input, as the tab it focuses on the second input, ignoring the first one, how does this occur if it is the same code for the two?

    $(document).on('keydown', 'input', function(enter) {
        if (enter.which == 13) {
            new_input(); } });

    $(document).on('keydown', 'input.val', function(tab) {
        if (tab.which == 9) {
            new_input(); } });

The function that creates a new input chain and focuses on the first one is this

        function new_input() {
        $('<x>'
            + '<input class="cod" id="focus' + rscn +'" placeholder="Código" />'
            + '<input class="desc" placeholder="Descrição" />'
            + '<div class="div"></div>'
            + '<input class="quant" placeholder="Quantidade" />'
            + '<input class="val" placeholder="Valor" />'
            + '<span id="remove_item" class="remove cursor_pointer display_none">+</span>'
        + '</x>').prependTo(scntDiv);
        $('#focus' + rscn).focus();
        rscn++;
        mais('itens_total');
        return false; }

The error is only on the tab, it skips the first input

    
asked by anonymous 05.04.2017 / 18:28

1 answer

1

edit

I decided after many but many frustrated tests, I will put the code for anyone who is going through the same case, had to insert a lot of functions inside the tab so that it worked right

        $(document).on('keydown', 'input.last', function(tab) {
            if(tab.which == 9) {
/* tira o foco do ultimo input */
                $(this).blur();
/* previne focar no proximo */
                event.preventDefault();
/* procura o primeiro input */
                $('input.first').first().focus();
/* cria nova linha de inputs */
                new_input(); } });

If anyone knows a smaller way to do this, I accept suggestions.

    
07.04.2017 / 18:48