-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
66 lines (57 loc) · 2 KB
/
script.js
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
const taskForm = document.getElementById('task-form');
const taskList = document.getElementById('task-list');
const themeToggle = document.getElementById('theme-toggle');
// Toggle Light/Dark Theme
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
themeToggle.textContent = document.body.classList.contains('dark-mode') ? '🌜' : '🌞';
});
// Task Storage and CRUD Operations
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
// Add Task
taskForm.addEventListener('submit', (e) => {
e.preventDefault();
const taskName = document.getElementById('task-name').value;
const taskDate = document.getElementById('task-date').value;
const taskPriority = document.getElementById('task-priority').value;
const taskId = Date.now();
const newTask = { id: taskId, name: taskName, date: taskDate, priority: taskPriority, completed: false };
tasks.push(newTask);
saveTasks();
displayTasks();
taskForm.reset();
});
// Display Tasks
function displayTasks() {
taskList.innerHTML = '';
tasks.forEach(task => {
const taskItem = document.createElement('li');
taskItem.classList.add('task-item');
taskItem.innerHTML = `
<span>${task.name} - ${task.date} (${task.priority})</span>
<button onclick="editTask(${task.id})" class="edit">✏️</button>
<button onclick="deleteTask(${task.id})" class="delete">🗑️</button>
`;
taskList.appendChild(taskItem);
});
}
// Save Tasks to Local Storage
function saveTasks() {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Delete Task
function deleteTask(id) {
tasks = tasks.filter(task => task.id !== id);
saveTasks();
displayTasks();
}
// Edit Task
function editTask(id) {
const task = tasks.find(t => t.id === id);
document.getElementById('task-name').value = task.name;
document.getElementById('task-date').value = task.date;
document.getElementById('task-priority').value = task.priority;
deleteTask(id);
}
// Load Tasks on Page Load
window.onload = displayTasks;