-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
44 lines (40 loc) · 1.55 KB
/
todo.js
File metadata and controls
44 lines (40 loc) · 1.55 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
class TodoList {
constructor () {
this.todoModel = ['todo satu', 'todo dua', 'todo tiga'];
this.todoForm = document.getElementById('todo-form');
this.todoContainer = document.getElementById('todo-container');
this.todoSubmit = document.getElementById('todo-submit');
// register event handle
this.todoSubmit.addEventListener('click', () => this.createTodo(this.todoForm[0].value));
}
createElement(value, index) {
const element = document.createElement("li");
element.className = 'list-group-item list-group-item-action';
element.innerText = String(value).toUpperCase();
element.setAttribute('data-index', index);
this.todoContainer.appendChild(element);
element.addEventListener('click', (event) => this.removeTodo(index, event.target));
}
createTodo(value) {
this.todoModel.push(value);
this.createElement(value, this.todoModel.length - 1);
}
/** @param {HTMLElement} currenNode */
removeElement(currenNode) {
this.todoContainer.removeChild(currenNode);
}
removeTodo(currentIndex, cureentdNode) {
const model = this.todoModel;
let selectedItem = this.todoModel[currentIndex];
let indexOf = this.todoModel.indexOf(selectedItem);
model.splice(indexOf, 1);
this.removeElement(cureentdNode);
}
makeList() {
this.todoModel.forEach((value, index) => {
this.createElement(value, index);
});
}
}
const todoList = new TodoList();
todoList.makeList();