Skip to content Skip to sidebar Skip to footer

Can't Delete Li From To-do List

I'm creating a to-do list application, and when the user clicks on a to-do that he/she has created, I want it to delete. But when I tested it out, it didn't delete. $(document).rea

Solution 1:

Two changes.

First,

$(".output ul li").on("click", "li", delete_todo);

Should be...

$(".output").on("click", "li", delete_todo);

You need to bind the click event to .output rather than an li, because the li elements don't exist yet when that event is created. Also, the previous code was looking for a click on an li that was a child of .output ul li.

Second,

letdelete_todo = () => {
    $(this).parent().remove();
}

Should be...

let delete_todo = (e) => {
    e.target.remove();
}

The context of $(this) would work in the click event itself, but not a function being called from it like this.

Post a Comment for "Can't Delete Li From To-do List"