-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
186 lines (124 loc) · 3.63 KB
/
script.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var ul = document.getElementById('list');
var li ;
var todos = new Array;
var check = new Array ;
var addButton = document.getElementById('add');
addButton.addEventListener('click',addItem);
var removeButton = document.getElementById('remove');
removeButton.addEventListener('click' , removeItem);
function addItem()
{
var input = document.getElementById('input');
var item = input.value;
ul = document.getElementById('list');
var textnode = document.createTextNode(item);
if(item.length > 1 )
{
//create li
li = document.createElement('li');
//create checkbox
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.setAttribute('id', 'check'); //to set id
//create label
var label = document.createElement('label');
label.setAttribute('for' , 'item');
//add these element to application
ul.appendChild(label);
li.appendChild(checkbox);
label.appendChild(textnode);
li.appendChild(label);
ul.insertBefore(li,ul.childNodes[0]);
li.className = 'visual';
input.value= ' ';
ui_mem();
}
}
function removeItem() {
//REMOVING FROM DISPLAY
li = ul.children;
for (let index = 0; index < li.length; index++) {
while (li[index] && li[index].children[0].checked) {
ul.removeChild(li[index]);
}
}
ui_mem();
}
function ui_mem (){
var li = ul.children;
//console.log(ul.children[]);
var todos = new Array;
var check = new Array ;
for(var i = 0 ; i < li.length ; i++)
{
todos[i] = li[i].children[1].innerText ;
check[i] = li[i].children[0].checked ;
}
localStorage.setItem('todo',JSON.stringify(todos)) ;
localStorage.setItem('check',JSON.stringify(check)) ;
}
//storage
function get_todos() {
var todos = new Array;
var todos_str = localStorage.getItem('todo');
if (todos_str !== null) {
todos = JSON.parse(todos_str);
}
return todos ;
}
function get_checked() {
var check = new Array;
var check_str = localStorage.getItem('check');
if (check_str !== null) {
check = JSON.parse(check_str);
}
return check ;
}
function show() {
var todos = get_todos();
var check = get_checked();
ul = document.getElementById('list');
for(var i=todos.length -1 ; i >=0 ; i--)
{
//create li
li = document.createElement('li');
var textnode = document.createTextNode(todos[i]);
//console.log(todos[i]);
//create checkbox
var checkbox = document.createElement('input');
checkbox.type = 'checkbox';
if(check[i])
{
checkbox.checked = true ;
}
checkbox.setAttribute('id', 'check'); //to set id
//create label
var label = document.createElement('label');
label.setAttribute('for' , 'item');
//add these element to application
ul.appendChild(label);
li.appendChild(checkbox);
label.appendChild(textnode);
li.appendChild(label);
ul.insertBefore(li,ul.childNodes[0]);
li.className = 'visual';
todos.value= ' ';
}
}
show();
$(":checkbox").change(function() {
if(this.checked) {
ui_mem();
}
});
$(":checkbox").change(function() {
if(!(this.checked)) {
ui_mem();
}
});
$('#input').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
addItem();
}
});