-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoList.js
126 lines (94 loc) · 3.59 KB
/
todoList.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const todoList = [];
const todoNameInput = document.querySelector('.js-name-input');
const todoDueDateInput = document.querySelector('.js-due-date-input');
function addTodo() {
const todoName = todoNameInput.value;
const todoDueDate = todoDueDateInput.value;
if (todoName || (todoName && todoDueDate)) {
todoList.push({
todoName,
todoDueDate
});
todoNameInput.value = '';
todoDueDateInput.value = '';
renderTodoList();
}
}
document.querySelector('.js-add-to-do-button')
.addEventListener('click', addTodo)
function renderTodoList() {
let todoHTML = '';
todoList.forEach((todo, index) => {
const { todoName, todoDueDate } = todo;
todoHTML += `
<div class="todo-name"><input class="checkbox" type="checkbox">${todoName}</div>
<div class="due-date">${todoDueDate}</div>
<span title="See more" class="dot">
<div class="update">
<h4 class="delete-todo" title="Delete">Delete</h4>
<h4 class="edit-todo" title="Edit">Edit</h4>
</div>
</span>
`;
});
document.querySelector('.js-todo-container').innerHTML = todoHTML;
document.querySelectorAll('.checkbox')
.forEach((checkbox, index) => {
let isChecked = true;
checkbox.addEventListener('click', () => {
if (isChecked) {
document.querySelectorAll('.todo-name')[index].style.textDecoration = "line-through";
document.querySelectorAll('.due-date')[index].style.textDecoration = "line-through";
isChecked = false;
} else {
document.querySelectorAll('.todo-name')[index].style.textDecoration = "none";
document.querySelectorAll('.due-date')[index].style.textDecoration = "none";
isChecked = true;
}
});
});
document.querySelectorAll('.dot')
.forEach((spanElem) => {
let isActive = true;
spanElem.addEventListener('click', () => {
if(isActive) {
spanElem.classList.add('dot-is-active');
spanElem.removeAttribute('title', 'see more')
spanElem.setAttribute('title', 'Hide')
isActive = false;
} else {
spanElem.classList.remove('dot-is-active');
spanElem.removeAttribute('title', 'Hide')
spanElem.setAttribute('title', 'See more')
isActive = true;
}
});
});
document.querySelectorAll('.delete-todo')
.forEach((deleteTodo, index) => {
deleteTodo.addEventListener('click', () => {
todoList.splice(index, 1);
renderTodoList();
});
});
document.querySelectorAll('.edit-todo')
.forEach((editTodo, index) => {
editTodo.addEventListener('click', () => {
todoNameInput.value = todoList[index].todoName;
todoDueDateInput.value = todoList[index].todoDueDate;
function addNewTodo() {
const newTodoName = todoNameInput.value;
const newTodoDueDate = todoDueDateInput.value;
todoList[index].todoName = newTodoName;
todoList[index].todoDueDate = newTodoDueDate;
document.querySelector('.js-add-to-do-button').removeEventListener('click', addNewTodo);
document.querySelector('.js-add-to-do-button').addEventListener('click', addTodo);
todoNameInput.value = '';
todoDueDateInput.value = '';
renderTodoList();
}
document.querySelector('.js-add-to-do-button').removeEventListener('click', addTodo);
document.querySelector('.js-add-to-do-button').addEventListener('click', addNewTodo);
});
});
}