This repository has been archived on 2026-05-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Web-Dev-Old/nodeJS/Learning/06/public/js/index.js
2024-12-31 00:12:59 +00:00

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));
}