-2
Hello, good evening friends, all good? I am studying and developing an All list,after adding an item(<li>) my <ol>, created an eventListener so that when you click on one of the items on the list, it will receive the class .selected, however, all items receive the class, not only the clicked, the worst, is that as I stuck to it, I am not able to proceed to implement the continuation, whether it would be by clicking on another item, it receive the class .selected while the last clicked would lose it.
As I am new in programming and mainly in jQuery, I am banging head here trying to understand how it would work to achieve the desired result, someone could give me a light by kindness?
Jsfiddle -> https://jsfiddle.net/ruqwgeh2/
Js:
// ADICIONA UM NOVO ITEM
$("#criar-tarefa").click(function () {
if ($("#texto-tarefa").val() === "") {
alert("Você deve digitar algo para ser adicionado a lista!");
} else {
$("#lista-tarefas").append(
`<li class="item-lista">${$("#texto-tarefa").val()}</li>`
);
$("#texto-tarefa").val("");
}
});
$("#lista-tarefas").on("click", "li", function () {
$("li").addClass("selected");
});
// REMOVE OS FINALIZADOS
$("#remover-finalizados").click(function () {
$(".completed").remove();
});
// TODO: SALVAR TAREFAS WEBSTORAGE
// $('#salvar-tarefas').click(function () {
// });
// REMOVE OS SELECIONADOS
$("#remover-selecionado").click(function () {
$(".selected").remove();
});
// APAGA TUDO
$("#apaga-tudo").click(function () {
$("#lista-tarefas").empty();
});
Gee Rafael, thank you so much for the help and congratulations on the didactic, really, I tried something like Function (Event), but I hadn’t given it because I didn’t know the . Targer, I can understand! Mainly by the way you explained, it was the first time I used the stack overflow and I was quite surprised, anyway, thank you very much!!!!
– Arthur Nunes
@Arthurnunes, I’m happy to help you, man, when I can help someone too. Hug!
– Rafael Rezende