|
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + const taskInput = document.getElementById("task-input"); |
| 3 | + const addTaskBtn = document.getElementById("add-task-btn"); |
| 4 | + const taskList = document.getElementById("task-list"); |
| 5 | + const clearAllBtn = document.getElementById("clear-all-btn"); |
| 6 | + |
| 7 | + addTaskBtn.addEventListener("click", function () { |
| 8 | + const taskText = taskInput.value.trim(); |
| 9 | + if (taskText !== "") { |
| 10 | + addTask(taskText); |
| 11 | + taskInput.value = ""; |
| 12 | + clearAllBtn.style.display = "inline-block"; // Show the button |
| 13 | + } |
| 14 | + }); |
| 15 | + |
| 16 | + function addTask(taskText) { |
| 17 | + const li = document.createElement("li"); |
| 18 | + |
| 19 | + li.innerHTML = ` |
| 20 | + <div class="flex justify-between items-center"> |
| 21 | + <span>${taskText}</span> |
| 22 | + <input type="datetime-local" class="expected-completion"> |
| 23 | + <button class="complete-btn bg-green-500 hover:bg-green-600 text-white py-1 px-2 rounded">Completed</button> |
| 24 | + <button class="delete-btn bg-red-500 hover:bg-red-600 text-white py-1 px-2 rounded">Delete</button> |
| 25 | + </div> |
| 26 | + `; |
| 27 | + |
| 28 | + const completeBtn = li.querySelector(".complete-btn"); |
| 29 | + completeBtn.addEventListener("click", function () { |
| 30 | + li.classList.toggle("completed"); |
| 31 | + }); |
| 32 | + |
| 33 | + const deleteBtn = li.querySelector(".delete-btn"); |
| 34 | + deleteBtn.addEventListener("click", function () { |
| 35 | + li.remove(); |
| 36 | + // Check the number of tasks to show/hide the "Clear All" button |
| 37 | + if (taskList.children.length === 0) { |
| 38 | + clearAllBtn.style.display = "none"; // Hide the button |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + // Use setTimeout to add the animation class after a slight delay |
| 43 | + setTimeout(function () { |
| 44 | + li.classList.add("task-entry"); |
| 45 | + }, 10); |
| 46 | + |
| 47 | + taskList.appendChild(li); |
| 48 | + } |
| 49 | + |
| 50 | + taskInput.addEventListener("keyup", function (event) { |
| 51 | + if (event.key === "Enter") { |
| 52 | + addTaskBtn.click(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + clearAllBtn.addEventListener("click", function () { |
| 57 | + taskList.innerHTML = ""; // Clear all tasks |
| 58 | + clearAllBtn.style.display = "none"; // Hide the button |
| 59 | + }); |
| 60 | +}); |
0 commit comments