Repository navigation
Expand file tree
/
Copy pathtoDoApp.js
More file actions
80 lines (54 loc) · 2.61 KB
/
Copy pathtoDoApp.js
File metadata and controls
80 lines (54 loc) · 2.61 KB
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
'use strict';
const newTask = document.getElementById('item');//I call the input
const ul = document.querySelector('ul');//I call the list where I am appending my tasks
const form= document.querySelector('form');
// addTaskButton = document.querySelector('#addtask');//I call the button to add task
const takeNap = document.getElementById('takeanap');//I call the button to cleanTasks
let itemsArray= localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')):[];
// let itemsArray;
// if (localStorage.getItem('items')){
// itemsArray = JSON.parse(localStorage.getItem('items'));
// } else {
// itemsArray = [];
// }
//this will save items in the localStorage of the browser according to key-value pair
localStorage.setItem('items', JSON.stringify(itemsArray));
const data = JSON.parse(localStorage.getItem('items')); //parse changes the JSON string to a JSON object so I can use the '.' notation.
console.log(data);
function makeList (text) {
const li = document.createElement('li');
li.textContent = text;
ul.appendChild(li);
}
// const saveTask = (e) =>{
// e.prevenDefault();
// itemsArray.push(newTask.value);
// console.log(itemsArray);
// //now I save Items in the localStorage
// localStorage.setItem('items', JSON.stringify(itemsArray));
// //now we append it in the ul by calling the function
// makeList(newTask.value);
// newTask.value= " ";
// };
form.addEventListener('submit',function (e){
e.preventDefault();
itemsArray.push(newTask.value);
console.log(itemsArray);
//now I save Items in the localStorage
localStorage.setItem('items', JSON.stringify(itemsArray));
//now we append it in the ul by calling the function
makeList(newTask.value);
newTask.value= " ";// this empties my input after each written task
});
data.forEach(item => {
makeList(item);
});
// form.addEventListener('submit',saveTask);
// now we erase the list if we desire
takeNap.addEventListener('click', function(){
localStorage.clear();
while(ul.firstChild){
ul.removeChild(ul.firstChild);
}
itemsArray = [];
})