0
Hello,
I tried several tricks (unfortunately) but I can’t find a solution to my idea.
I have an "app" that creates a to-do list, only I’d like to add to my list a different id for each div
added within the div
leading.
var main = function(){
$('button').click(function(){
var tarefa = $('input[name=tarefa]').val();
var conferir = $('.item').text();
if(tarefa==conferir){
alert("Tarefa já foi adicionada!");
}
//Adiciona um div com o id item dentro do div com classe lista-tarefa
$('.lista-tarefa').append('<div id="item">' + tarefa + '</div>');
$('input[name=tarefa]').val('');
})
$(document).on('click', '.item', function(){
if (confirm("Deseja apagar?")){
$(this).remove();
}
})
}
$(document).ready(main)
In the comment part he will always add a div
with the same id, but I’d like to add with a different id so I can do a repeat validation when the user enters the task.
In practice I would like to add a div with id="item1"
, id="item2"
, and so on.
Vinicius, I tried to use it in his own way but I failed to declare the variable counter, because when I execute the code he says that the variable does not exist. And if I start it with 1 or 0 it always adds the same number.
– RFL
I didn’t need to add a class at the end, I was using classes for manipulation anyway. In the part where Voce puts the $("#" + id).on... Does not work uncaugth Referenceerror: id is not defined.
– RFL
In my code the variable
contador
is declared and initialised, andid
in its scope of use. I will highlight the statements of these variables in my code, as it probably lacked copy something to your code.– Vinícius Gobbo A. de Oliveira
I removed the final snippet of the answer. Got confused hehehe.
– Vinícius Gobbo A. de Oliveira