0
I am solving some exercises in javascript and I came across a situation that I can not solve.
I have a small "app" that creates a list of tasks, so far it works normally, I can add a new div within the main div. however, I need to check if the name already exists within this main div, below.;
var main = function(){
$('button').click(function(){
var tarefa = $('input[name=tarefa]').val();
var conferir = $('.item').text();
if(tarefa==conferir){
alert("Tarefa já foi adicionada!");
}
$('.lista-tarefa').append('<div class="item">' + tarefa + '</div>');
$('input[name=tarefa]').val('');
})
$(document).on('click', '.item', function(){
if (confirm("Deseja apagar?")){
$(this).remove();
}
})
}
$(document).ready(main)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="painel">
<label for="tarefa">Tarefa</label>
<input type="text" name="tarefa" />
<button id="btn">ADD</button>
</div>
<div class="lista-tarefa">
</div>
When I click the button to add a task, it informs that the task has actually been added but adds it again.
Lacked a
else
after theif
– Franchesco
The above example is comparing only the first task.
– Tobias Mesquita
you could also put the rest of the code in an LSE{}.
– Wilson Rosa Gomes