25 lines
610 B
JavaScript
25 lines
610 B
JavaScript
// Function to delete a todo item
|
|
function deleteTodo(todo) {
|
|
fetch("/todo", {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ todo }),
|
|
})
|
|
.then((response) => {
|
|
if (response.ok) {
|
|
// Remove the list item and button from the DOM
|
|
const listItem = [...document.querySelectorAll("li")].find((li) =>
|
|
li.textContent.trim().startsWith(todo)
|
|
);
|
|
if (listItem) {
|
|
listItem.remove();
|
|
}
|
|
} else {
|
|
console.error("Failed to delete the todo item.");
|
|
}
|
|
})
|
|
.catch((error) => console.error("Error deleting todo:", error));
|
|
}
|