-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
104 lines (94 loc) · 2.78 KB
/
todo.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
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
// TODO LIST
// ? ADD NEW TASK
const addButton = document.querySelector('.push');
addButton.addEventListener('click', function (event) {
event.preventDefault();
const inputText = document.querySelector('.newTask input');
let inputTextValue = inputText.value;
let inputTextLength = inputTextValue.length;
if (inputTextLength !== 0)
{
document.querySelector('#tasks').innerHTML += `
<li class="task">
<button class="completeTask">
<i class="fa-solid fa-check"></i>
</button>
<span class="taskName">
${inputTextValue}
</span>
<div class="taskBtn">
<button class="edit">
<i class="fa-regular fa-pen-to-square"></i>
</button>
<button class="delete">
<i class="fa-solid fa-trash-can"></i>
</button>
</div>
</li>
`;
inputText.value = '';
edit();
complete();
remove();
} else
{
alert('Invalid input');
}
});
// ? EDIT TASK
const edit = () => {
let editTasks = Array.from(document.querySelectorAll('.edit'));
editTasks.forEach(function (editTask) {
editTask.addEventListener('click', function () {
let task = this.parentNode.parentNode;
let editButton = task.querySelector('.edit');
let deleteButton = task.querySelector('.delete');
let taskName = task.querySelector('.taskName');
let taskNameValue = taskName ? taskName.textContent : '';
let inputEdit = document.createElement('input');
inputEdit.value = taskNameValue;
if (taskName)
{
task.replaceChild(inputEdit, taskName);
}
editButton.innerHTML = '<i class="fa-solid fa-check"></i>';
editButton.addEventListener('click', function () {
let newName = inputEdit.value;
let newTaskName = document.createElement('span');
newTaskName.setAttribute('class', 'taskName');
newTaskName.textContent = newName;
if (task.contains(inputEdit))
{
task.replaceChild(newTaskName, inputEdit);
editButton.innerHTML = '<i class="fa-regular fa-pen-to-square"></i>';
} else
{
editButton.innerHTML = '<i class="fa-solid fa-check"></i>';
}
});
});
});
};
edit();
// ? COMPLETED TASK
const complete = () => {
let completeTask = Array.from(document.querySelectorAll('.completeTask'));
for (let i = 0; i < completeTask.length; i++)
{
completeTask[i].addEventListener('click', function () {
this.classList.toggle('completed');
});
}
};
complete();
// ? DELETE TASK
const remove = () => {
let deleteTasks = Array.from(document.querySelectorAll('.delete'));
for (let i = 0; i < deleteTasks.length; i++)
{
deleteTasks[i].addEventListener('click', function () {
this.parentNode.parentNode.remove();
});
}
};
remove();