I'm solving some exercises in javascript and I came across a situation I can not solve.
I have a small "application" that creates a task list, so far it works normally, I can add a new div inside the main div. however, I need to check if the name already exists inside 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><divclass="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 already been added but adds it again.