-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
49 lines (38 loc) · 1.17 KB
/
Copy pathmain.js
File metadata and controls
49 lines (38 loc) · 1.17 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
let todos = [];
function capitalize(text) {
return `${text[0].toUpperCase()}${text.slice(1)}`;
}
function createLi(text, id) {
const element = document.createElement('li');
element.innerHTML = text;
const removeButton = document.createElement('button');
removeButton.innerHTML = 'X';
removeButton.id = id;
removeButton.addEventListener('click', e => removeTodo(e.target.id));
element.appendChild(removeButton);
return element;
}
function createTodoList(todos) {
const todoList = document.createElement('ul');
todos.map((todo, id) => todoList.appendChild(createLi(todo, id)));
return todoList;
}
function render() {
document.querySelector('.container').innerHTML = null;
document.querySelector('.container').appendChild(createTodoList(todos));
}
function addTodo(event) {
event.preventDefault();
const todoInput = document.querySelector('#todoInput');
const newTodo = capitalize(todoInput.value);
if (newTodo) {
todos = [...todos, newTodo];
todoInput.value = '';
render();
}
}
function removeTodo(id) {
todos.splice(id, 1);
render();
}
document.addEventListener('load', render());