IF condition in jQuery

0

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(); }
  })
    
asked by anonymous 09.04.2015 / 04:18

1 answer

1

Notice that you are using the 1.2.3 version of jQuery. This version is very old and does not yet have the .on() method introduced in 1.7 . Switch to a more current version and it will work.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/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>

jsFiddle: link

    
09.04.2015 / 08:01