-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (76 loc) · 2.4 KB
/
index.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
var toDoContainer = document.getElementById("toDoContainer");
function sabe() {
if (localStorage.length === 0) {
localStorage.setItem("0", "0");
}
if (localStorage.length > 1) {
document.querySelector("h3").style.display = "none";
}
var indexStorage = Number(localStorage[0]);
for (i = 1; i < localStorage.length; i++) {
if (localStorage.getItem(indexStorage)) {
var toDoShell = document.createElement("div");
toDoShell.classList.add("toDoShell");
toDoShell.id = indexStorage;
toDoContainer.appendChild(toDoShell);
deleteButton(indexStorage, toDoShell);
var toDoText = document.createElement("span");
toDoShell.appendChild(toDoText);
toDoText.innerHTML = localStorage.getItem(indexStorage);
toDoText.id = indexStorage;
indexStorage--;
} else {
indexStorage--;
i--;
}
}
}
window.onload = function () {
sabe();
};
function deleteButton(indexStorage, toDoShell) {
var toDoDelete = document.createElement("div");
toDoDelete.classList.add("toDoDelete");
toDoDelete.id = indexStorage;
toDoDelete.innerText = "Borrar";
toDoShell.appendChild(toDoDelete);
toDoDelete.addEventListener("click", deleteTodo, false);
}
function createTodo() {
document.querySelector("h3").style.display = "none";
var indexStorage = Number(localStorage[0]);
indexStorage++;
var toDoShell = document.createElement("div");
toDoShell.classList.add("toDoShell");
toDoShell.id = indexStorage;
toDoContainer.appendChild(toDoShell);
deleteButton(indexStorage, toDoShell);
var toDoText = document.createElement("span");
toDoShell.appendChild(toDoText);
toDoText.innerHTML = input.value;
toDoText.id = indexStorage;
localStorage.setItem(indexStorage, input.value);
localStorage.setItem("0", indexStorage);
input.value = "";
}
var button = document.querySelector("#addButton");
var input = document.querySelector("#toDoInput");
button.addEventListener("click", () => {
if (input.value !== "") {
createTodo();
}
});
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13 && input.value !== "") {
createTodo();
}
});
function deleteTodo() {
localStorage.removeItem(`${this.id}`);
var d = document.getElementById("toDoContainer");
var d_h = document.getElementById(`${this.id}`);
d.removeChild(d_h);
if (localStorage.length === 1) {
document.querySelector("h3").style.display = "";
}
}