-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.html
More file actions
153 lines (146 loc) · 4.01 KB
/
Copy pathtodo.html
File metadata and controls
153 lines (146 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My To-Do List</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: pink;
margin: 20px;
}
#container {
background-color: whitesmoke;
padding: 20px;
max-width: 400px;
margin: auto;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
}
/* New: Counter styling */
#counter {
text-align: center;
margin: 10px 0;
font-weight: bold;
}
#todoInput {
width: 70%;
padding: 10px;
font-size: 16px;
}
#addBtn {
padding: 10px;
font-size: 16px;
}
ul {
list-style: none;
padding: 0;
margin-top: 20px;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
cursor: pointer;
}
li.completed {
text-decoration: line-through;
color: gray;
}
</style>
</head>
<body>
<div id="container">
<h2>What Should I Do? -UwU-</h2>
<!-- Counter element -->
<p id="counter">Tasks Left: 0</p>
<input type="text" id="todoInput" placeholder="Enter a new task">
<button id="addBtn">Add Task</button>
<ul id="todoList"></ul>
</div>
<script>
// Grab DOM elements
const input = document.getElementById('todoInput');
const addBtn = document.getElementById('addBtn');
const todoList = document.getElementById('todoList');
const counterDisplay = document.getElementById('counter');
// -------------------------------
// Local Storage Functions
// -------------------------------
// Save tasks to localStorage
function saveTasks() {
const tasks = [];
const listItems = todoList.querySelectorAll('li');
listItems.forEach(li => {
tasks.push({
text: li.textContent,
completed: li.classList.contains('completed')
});
});
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Load tasks from localStorage
function loadTasks() {
const tasks = JSON.parse(localStorage.getItem('tasks'));
if (tasks) {
tasks.forEach(task => {
createTaskElement(task.text, task.completed);
});
}
updateCounter();
}
// -------------------------------
// Counter Function
// -------------------------------
// Update the counter display based on non-completed tasks
function updateCounter() {
const listItems = todoList.querySelectorAll('li');
let count = 0;
listItems.forEach(li => {
if (!li.classList.contains('completed')) {
count++;
}
});
counterDisplay.textContent = 'Tasks Left: ' + count;
}
// -------------------------------
// Task Creation & Interaction
// -------------------------------
// Create a new task element in the DOM and attach event listeners
function createTaskElement(taskText, completed = false) {
const li = document.createElement('li');
li.textContent = taskText;
if (completed) {
li.classList.add('completed');
}
// Toggle the 'completed' state on click
li.addEventListener('click', function() {
li.classList.toggle('completed');
saveTasks(); // Save changes when toggled
updateCounter(); // Update the counter
});
todoList.appendChild(li);
}
// Add a new task using the text from the input field
function addTask() {
const taskText = input.value.trim();
if (taskText !== "") {
createTaskElement(taskText);
input.value = "";
saveTasks(); // Save the new task
updateCounter(); // Update the counter
}
}
// Event Listeners for button click and pressing "Enter"
addBtn.addEventListener('click', addTask);
input.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
addTask();
}
});
// Load tasks from localStorage when the page loads
window.onload = loadTasks;
</script>
</body>
</html>