-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos.js
52 lines (37 loc) · 1.17 KB
/
todos.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
var listElement = document.querySelector('#app ul');
var inputElement = document.querySelector('#app input');
var buttonElement = document.querySelector('#app button');
var todos = JSON.parse(localStorage.getItem('list_todos')) || [];
function renderTodos() {
listElement.innerHTML = '';
for(todo of todos) {
var todoElement = document.createElement('li');
var todoText = document.createTextNode(todo);
var linkElement = document.createElement('a');
linkElement.setAttribute('href','#');
var pos = todos.indexOf(todo);
linkElement.setAttribute('onclick','deleteTodo(' + pos + ')');
var linkText = document.createTextNode('Excluir');
linkElement.appendChild(linkText);
todoElement.appendChild(todoText);
todoElement.appendChild(linkElement);
listElement.appendChild(todoElement);
}
}
renderTodos();
function addTodo() {
var todoText = inputElement.value;
todos.push(todoText);
inputElement.value = '';
renderTodos()
saveToStorage()
}
buttonElement.onclick = addTodo;
function deleteTodo(pos) {
todos.splice(pos, 1);
renderTodos();
saveToStorage()
}
function saveToStorage() {
localStorage.setItem('list_todos', JSON.stringify(todos));
}