0
I am trying to add an IF condition to my study exercise in jQuery and I have a small problem.
the code below creates a short list of tasks and when clicked on 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>
<div class="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(); }
})
I didn’t really understand what the problem is and how to reproduce it.
– Maniero
I tried to add an IF to ask the user if he wants to delete a DIV and that IF doesn’t work.
– RFL
I tested it here and it worked normally, see if that’s what you want: http://jsfiddle.net/mzrhefh0/
– Rafael Almeida
Almeida, it did work. in codepen.io is not working.
– RFL