From f5f2cfcf1c57897d752525da90581fb5f2684d29 Mon Sep 17 00:00:00 2001 From: WhyKorp <117651228+whykorp@users.noreply.github.com> Date: Mon, 11 Mar 2024 06:40:42 +0100 Subject: [PATCH] Update scripttodo.js --- ruty/js/scripttodo.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ruty/js/scripttodo.js b/ruty/js/scripttodo.js index 2cded72..9e2e44c 100644 --- a/ruty/js/scripttodo.js +++ b/ruty/js/scripttodo.js @@ -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")) || [];