-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (40 loc) · 1.26 KB
/
Copy pathscript.js
File metadata and controls
48 lines (40 loc) · 1.26 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
const taskInput = document.getElementById('taskInput');
const addBtn = document.getElementById('addBtn');
const clearBtn = document.getElementById('clearBtn');
const taskList = document.getElementById('taskList');
function addTask() {
const taskText = taskInput.value.trim();
if (taskText === '') {
alert("⚠️ Please enter a task!");
return;
}
const li = document.createElement('li');
li.textContent = taskText;
li.addEventListener('click', () => {
li.classList.toggle('completed');
});
const deleteBtn = document.createElement('button');
deleteBtn.textContent = '🗑️ Delete';
deleteBtn.className = 'deleteBtn';
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation(); // this is to prevent it from marking complete
li.remove();
});
li.appendChild(deleteBtn);
taskList.appendChild(li);
taskInput.value = '';
}
function clearAllTasks() {
if (taskList.children.length === 0) {
alert("⚠️ No tasks to clear!");
return;
}
if (confirm("Are you sure you want to delete all tasks? ❌")) {
taskList.innerHTML = '';
}
}
addBtn.addEventListener('click', addTask);
taskInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') addTask();
});
clearBtn.addEventListener('click', clearAllTasks);