Re-enable jquery function after function call

0

Good afternoon, I have the following code that inserts inputs into a div when a "NEW" button is clicked which also takes the direct focus to the first field on the line and can also happen by pressing enter, the problem is, only the enter works on the first line, the lines that are inserted do not work enter.

$(document).ready(function() {
var scntDiv = $('#more_item');
var rscn = $('#more_item x').size() + 1;
new_input();
input_keydown();

$('#more_item_add').on('click', function() {
    new_input(); });

function input_keydown() {
    $('input').keydown(function(e) {
        if (e.which == 13) {
            new_input(); } }); }

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

$('#remove_item').live('click', function() {
    if(rscn > 1) {
        $(this).parents('x').remove();
        rscn--; }
    return false; });

$('#print').on('click', function() {
    window.print(); });

$('#reset').on('click', function() {
    location.reload(); }); });

In the case, it is difficult because I wanted to press the enter until the end, that is, I fill in the first line and press the enter, then jump to the second and press the enter to the third and so on, but it only works at first.

<script src='jquery_1.8.3.js'></script>
<div class='inputs'>
    <div id='more_item_add' class='button cursor_pointer display_none'>Novo</div>
    <div id='print' class='button cursor_pointer display_none'>Imprimir</div>
    <div id='reset' class='button cursor_pointer display_none'>Resetar</div>
    <div id='more_item'></div>
</div>

I do not know if it is the best format, I accept suggestions, but I do not like to wait for the answer without testing, so I searched a lot even after asking and found a solution that helped exactly the way I needed it,

function input_keydown() {
$('input').keydown(function(e) {
    if (e.which == 13) {
        new_input(); } }); }

by this part

        $(document).on('keydown', 'input', function(e) {
            if (e.which == 13) {
                new_input(); } });
    
asked by anonymous 04.04.2017 / 17:01

1 answer

0

Using this excerpt, I got a nice result but still accepted suggestions

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

and in the input input function I changed from append to prepend

function new_input() {
    $('<x>'
        + '<input class="cod" placeholder="Código" />'
        + '<input class="desc" placeholder="Descrição" />'
        + '<input id="teste_de_calculo quantidade" class="quant" placeholder="Quantidade" />'
        + '<input id="teste_de_calculo resultado" class="val" placeholder="Valor" />'
        + '<span class="remove cursor_pointer display_none">+</span>'
    + '</x>').prependTo(scnDiv);
    $('input').first().focus();
    rscnDiv++; } });
    
07.04.2017 / 20:34