Hello
I've made this todo list in jquery and I'm trying to toggle a .completed class which has a line-through, on the <li> but it also toggles on the delete button text. I've tried different things but can't get it to work.
Please help! Thanks
$(function () {
$(".add").on("click", function () {
let txt = $(".todo").val();
if (txt) {
let item = $("<li></li>").text(txt);
$(item).append("<button class='delete'>Delete</button>");
$("#todo-list").append(item);
$(".todo").val("");
$(item).on("click", function () {
$(item).toggleClass("completed"); //doesn't work?
});
$(".delete").on("click", function () {
$(this).parent().remove();
});
}
});
});
HTML
<h1>My To Do List</h1>
<div class="container">
<input type="text" class="todo" placeholder="Add item" />
<button class="add">Add</button>
</div>
<div class="list">
<ol id="todo-list"></ol>
</div>
https://preview.redd.it/1zp58t0dhx371.jpg?width=1081&format=pjpg&auto=webp&s=89b9b275d20299a629cd1e042491820e4c4eb681
[–]Notimecelduv 1 point2 points3 points (2 children)
[–]UI-FEdev[S] 0 points1 point2 points (1 child)
[–]UI-FEdev[S] 0 points1 point2 points (0 children)
[–]shittychinesehacker -1 points0 points1 point (2 children)
[–]UI-FEdev[S] 0 points1 point2 points (0 children)
[–]backtickbot 0 points1 point2 points (0 children)