-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (42 loc) · 1.56 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
var form = document.addItem;
var listArr = [];
//checking for localstorage key value
if(localStorage.list){
listArr = JSON.parse(localStorage.list);
}
// calling function to display items in local storage
displayList();
//adding items to localstorage and displaying on page on click
form.submit.addEventListener("click", function(event){
event.preventDefault();
listArr.push({'message': form.title.value, 'id': listArr.length});
localStorage.list = JSON.stringify(listArr);
listArr = JSON.parse(localStorage.list);
console.log(localStorage.list,listArr)
form.title.value = "";
displayList();
})
// adds list item to page html within <ul>
function displayList(){
let itemLabel= '';
for(let i=0;i<listArr.length;i++){
itemLabel += '<li><div class="listLabel">'+ listArr[i].message+ '</div><div class="listButtons"><button onclick="edit('+listArr[i].id+')">edit</button><button onclick="remove('+listArr[i].id+')">X</button></div></li> ';
}
document.getElementById("list").innerHTML = itemLabel;
}
//removes item from localstorage and displays updated list
function remove(id){
listArr= listArr.filter(function(value, index, arr){
return value.id != id;
});
localStorage.list = JSON.stringify(listArr);
displayList();
}
// displays value to be edited in input box to be resubmitted as a new item and removes the current outdated item
function edit(id){
let item = listArr.filter(function(value, index, arr){
return value.id == id;
});
form.title.value = item[0].message;
remove(id);
}