-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (49 loc) · 1.62 KB
/
script.js
File metadata and controls
65 lines (49 loc) · 1.62 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
const addBtn = document.getElementById('add-btn');
const taskInput = document.getElementById('task-input');
const taskList = document.getElementById('task-list');
const addTask = () => {
const val = taskInput.value.trim();
if(val.length === 0) return;
const li = document.createElement('li');
li.className = 'task-item';
const checkbox = document.createElement('span');
checkbox.className = 'custom-checkbox';
const text = document.createElement('span');
text.className = 'task-text';
text.textContent = val;
// ✅ Edit button
const editBtn = document.createElement('button');
editBtn.className = 'edit-btn';
editBtn.textContent = '✏️';
const delBtn = document.createElement('button');
delBtn.className = 'delete-btn';
delBtn.textContent = '❌';
// Toggle complete
checkbox.addEventListener('click', () => {
checkbox.classList.toggle('checked');
text.classList.toggle('completed');
});
// Delete task
delBtn.addEventListener('click', () => {
taskList.removeChild(li);
});
// ✅ Edit task
editBtn.addEventListener('click', () => {
// Fill input with current text
taskInput.value = text.textContent;
taskInput.focus();
// Remove the old task temporarily
taskList.removeChild(li);
// When user presses Enter or clicks Add, the updated text will replace it
});
li.appendChild(checkbox);
li.appendChild(text);
li.appendChild(editBtn);
li.appendChild(delBtn);
taskList.appendChild(li);
taskInput.value = '';
};
addBtn.addEventListener('click', addTask);
taskInput.addEventListener('keyup', (e) => {
if(e.key === 'Enter') addTask();
});