Keydown function skipping an input on TAB

Asked

Viewed 495 times

1

Good afternoon, I have a function that when I hit enter it creates a new series of inputs in the first line pushing the filled ones down, I made that when I hit the tab in the last one the same function also occurs, however, when I hit enter, regardless of the input that is, it creates another input chain by focusing on the first input, exactly as I programmed, perfect, already on the tab, which is only to work on the last input that in the case is already like this, the error is, with enter, it focuses on the first input, as the tab it focuses on the second input, ignoring the first, as this occurs 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 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 in tab, it skips the first input

  • Use the answer area to post your solution, do not use the statement itself. If possible, remove the answer from the question and convert it to an answer,d and fact.

  • I don’t understand, when I’m gonna do this, the stack itself tells me not to do it, asks me to edit the question, believe who after all

  • @flourigh the stack itself or someone who participates in the stack? What the person must have said is about adding details of doubt in the response field, you who must have misunderstood =)

  • When answering your own question, the website says: "If you want to provide more details about your problem, edit your question.". In this case, an answer is not detail of the question, so you can post without fear. There is this because many members think it’s a forum and try to continue the discussion by posting answers, rather than inserting the new details directly into the question.

  • Valew, I did what you recommended.

1 answer

1


Edit

I solved after many, but many tests frustrated, I will put the code to whoever is going through the same case, I had to insert a lot of functions inside the tab to make it work 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 minor way to do this, I accept suggestions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.