-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
88 lines (70 loc) · 2.21 KB
/
main.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
const container = document.querySelector('.todoOutput');
var inputValue = document.querySelector('.input');
const add = document.querySelector('.add');
if(window.localStorage.getItem("todos") == undefined){
var todos = [];
window.localStorage.setItem("todos", JSON.stringify(todos));
}
var todosEX = window.localStorage.getItem("todos");
var todos = JSON.parse(todosEX);
class item{
constructor(name){
this.createItem(name);
}
createItem(name){
var itemBox = document.createElement('div');
itemBox.classList.add('item');
var input = document.createElement('input');
input.type = "text";
input.disabled = true;
input.value = name;
input.classList.add('item_input');
var edit = document.createElement('button');
edit.classList.add('edit');
edit.innerHTML = "EDIT";
edit.addEventListener('click', () => this.edit(input, name));
var remove = document.createElement('button');
remove.classList.add('remove');
remove.innerHTML = "REMOVE";
remove.addEventListener('click', () => this.remove(itemBox, name));
container.appendChild(itemBox);
itemBox.appendChild(input);
itemBox.appendChild(edit);
itemBox.appendChild(remove);
}
edit(input, name){
if(input.disabled == true){
input.disabled = !input.disabled;
}
else{
input.disabled = !input.disabled;
let indexof = todos.indexOf(name);
todos[indexof] = input.value;
window.localStorage.setItem("todos", JSON.stringify(todos));
}
}
remove(itemBox, name){
itemBox.parentNode.removeChild(itemBox);
let index = todos.indexOf(name);
todos.splice(index, 1);
window.localStorage.setItem("todos", JSON.stringify(todos));
}
}
add.addEventListener('click', check);
window.addEventListener('keydown', (e) => {
if(e.which == 13){
check();
}
})
function check(){
if(inputValue.value != ""){
new item(inputValue.value);
todos.push(inputValue.value);
window.localStorage.setItem("todos", JSON.stringify(todos));
inputValue.value = "";
}
}
for (var v = 0 ; v < todos.length ; v++){
new item(todos[v]);
}
new item("profit!!!");