Update scripttodo.js

This commit is contained in:
WhyKorp
2024-03-11 06:40:42 +01:00
committed by GitHub
parent a5800a2b21
commit f5f2cfcf1c
+17 -1
View File
@@ -34,7 +34,12 @@ document.addEventListener("DOMContentLoaded", function() {
const todoText = newTodoInput.value.trim();
if (todoText !== "") {
const todoItem = document.createElement("li");
todoItem.textContent = todoText;
const todoCheckbox = document.createElement("input");
todoCheckbox.type = "checkbox";
const todoSpan = document.createElement("span");
todoSpan.textContent = todoText;
todoItem.appendChild(todoCheckbox);
todoItem.appendChild(todoSpan);
todoList.appendChild(todoItem);
newTodoInput.value = "";
}
@@ -60,6 +65,17 @@ document.addEventListener("DOMContentLoaded", function() {
}
});
todoList.addEventListener("change", function(event) {
if (event.target.type === "checkbox") {
const todoItem = event.target.parentNode;
if (event.target.checked) {
todoItem.classList.add("completed");
} else {
todoItem.classList.remove("completed");
}
}
});
// Function to load existing todos from localStorage
function loadTodos() {
const savedTodos = JSON.parse(localStorage.getItem("todos")) || [];