Input Text with jQuery

1

Why can not I type text in the input ".add-edit" that was created with jQuery ?

$(function(){
    function onTarefaDeleteClick(){
        $(this).parent('.tarefa-item').hide('slow', function(){
            $(this).remove();
        });
    }
    $('.tarefa-delete').click(onTarefaDeleteClick);
});
$(function(){
    function onTarefaItemClick(){
        var text = $(this).children('.tarefa-texto').text();
        var html = '<input type="text" ' + 'class="tarefa-edit" value="' + text + '">';
        $(this).html(html);
        $('.tarefa-edit').keydown(onTarefaEditKeydown);
    }
    $('.tarefa-item').click(onTarefaItemClick); //
});
function onTarefaEditKeydown(event){
    if(event.which === 13){
        savePendingEdition($(this));
    }
}
function savePendingEdition(tarefa){
    console.log('...');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="todo">
    <h2>TODO List</h2>
    <input type="text" id="tarefa">
    <div id="tarefa-lista">
        <div class="tarefa-item">
            <div class="tarefa-texto">Comprar pão</div>
            <div class="tarefa-delete"></div>
            <div class="clear"></div>
        </div>
    </div>
</div>
    
asked by anonymous 12.07.2018 / 16:52

1 answer

2

What is happening that you have associated the onTarefaItemClick function with the click event of the <div class="tarefa-item"> tag, so how much do you click the <input type="text" class="tarefa-edit" value=""> tag that is inside the <div class="tarefa-item"> tag and the onTarefaItemClick tag is called again by recreating the text box, you disable onclick event > tag <div class="tarefa-item"> After creating the text box the problem will not occur:

$(function(){
    function onTarefaItemClick(){
        $('.tarefa-item').unbind();
        var text = $(this).children('.tarefa-texto').text();
        var html = '<input type="text" ' + 'class="tarefa-edit" value="' + text + '">';
        $(this).html(html);
        $('.tarefa-edit').keydown(onTarefaEditKeydown);
    }
    $('.tarefa-item').click(onTarefaItemClick); //
});
    
12.07.2018 / 18:24