I'm trying to add an IF condition in my jQuery study exercise and I'm having a little problem.
The code below creates a short list of tasks and when clicked on top of the div created by jquery it is deleted.
var main = function(){
$('#button').click(function(){
var item = $('input[name=txtTarefa]').val();
$('.lista').append('<div class="item">' + item + '</div>');
})
$(document).on('click', '.item', function(){
$(this).remove();
})
}
$(document).ready(main)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script><divclass="painel">
<label for="acao">Tarefa:</label>
<input type="text" name="txtTarefa" class="tarefa" />
<button name="btn" id="button">Adicionar</button>
</div>
<br />
<div class="lista">
</div>
The problem is that if I try to create a condition before deleting the item the condition is not executed. for example;
$(document).on('click', '.item', function(){
var apagar = confirm("Deseja apagar a tarefa?");
if (apagar){
$(this).remove(); }
})