-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (65 loc) · 2.44 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
import _ from 'lodash';
window.addEventListener('load', toDoList);
arrayOfTasks = [];
function toDoList() {
if(localStorage.getItem('tasks') != null){
arrayOfTasks = localStorage.getItem('tasks').split(',')
arrayOfTasks.sort()
}
console.log(localStorage)
let form = document.createElement('input');
form.type = 'text';
document.body.append(form);
let button = document.createElement('button');
button.type = 'button';
button.innerHTML = 'addNew'
document.body.append(button)
let buttonClr = document.createElement('button');
buttonClr.type = 'button';
buttonClr.innerHTML = 'clear'
document.body.append(buttonClr)
buttonClr.addEventListener('click', clear);
let container = document.createElement('div');
container.id = 'cont';
document.body.append(container);
let list = document.createElement('ul');
list.id = 'ls';
document.getElementById('cont').appendChild(list);
console.log(arrayOfTasks)
for (let i = 0; i < arrayOfTasks.length; i++) {
let task = document.createElement('li');
task.innerHTML = arrayOfTasks[i];
document.getElementById('ls').appendChild(task);
}
button.addEventListener('click', adder);
window.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
adder()
}})
}
function adder(){
if(document.querySelector('input').value != '' && document.querySelector('input').value === document.querySelector('input').value.trim()){
while (document.getElementById('ls').firstChild) {
document.getElementById('ls').removeChild(document.getElementById('ls').firstChild);
}
arrayOfTasks.push(document.querySelector('input').value);
arrayOfTasks.sort();
localStorage.setItem('tasks', arrayOfTasks)
document.querySelector('input').value = '';
arrayOfTasks = localStorage.getItem('tasks').split(',')
for (let i = 0; i < arrayOfTasks.length; i++) {
let task = document.createElement('li');
task.innerHTML = arrayOfTasks[i];
document.getElementById('ls').appendChild(task);
}
console.log(arrayOfTasks)
console.log(localStorage)
}
}
function clear(){
while (document.getElementById('ls').firstChild) {
document.getElementById('ls').removeChild(document.getElementById('ls').firstChild);
}
localStorage.clear()
document.location.reload()
}