-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (61 loc) · 2.07 KB
/
Copy pathindex.js
File metadata and controls
87 lines (61 loc) · 2.07 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
81
82
83
84
85
// defining all element
let add = document.getElementById('add');
// adding eventListener
add.addEventListener('click',getAndupdate);
// declare function
function getAndupdate(){
title = document.getElementById('title').value;
desc = document.getElementById('desc').value;
if(localStorage.getItem('ItemJson') == null){
ItemJsonArray = [];
ItemJsonArray.push([title,desc]);
localStorage.setItem('ItemJson',JSON.stringify(ItemJsonArray));
}else{
ItemJsonArraystr = localStorage.getItem('ItemJson');
ItemJsonArray = JSON.parse(ItemJsonArraystr);
ItemJsonArray.push([title,desc]);
localStorage.setItem('ItemJson',JSON.stringify(ItemJsonArray));
}
update();
document.getElementById('title').value = '';
document.getElementById('desc').value = '';
}
function update(){
title = document.getElementById('title').value;
desc = document.getElementById('desc').value;
if(localStorage.getItem('ItemJson') == null){
ItemJsonArray = [];
localStorage.setItem('ItemJson',JSON.stringify(ItemJsonArray));
}else{
ItemJsonArraystr = localStorage.getItem('ItemJson');
ItemJsonArray = JSON.parse(ItemJsonArraystr);
}
// populate the table
tableBody = document.getElementById('tableBody');
str = '';
ItemJsonArray.forEach((element,index) => {
str += `
<tr>
<th scope="row">${index + 1}</th>
<td>${element[0]}</td>
<td>${element[1]}</td>
<td><button class="btn btn-primary" onclick="deleted(${index})">Delete</button></td>
</tr>`;
});
tableBody.innerHTML = str;
}
function deleted(itemIndex){
ItemJsonArraystr = localStorage.getItem('ItemJson');
ItemJsonArray = JSON.parse(ItemJsonArraystr);
ItemJsonArray.splice(itemIndex,1);
localStorage.setItem('ItemJson',JSON.stringify(ItemJsonArray));
update();
}
// storage clear
function clearList(){
var flag = confirm("Are you sure ??");
if(flag == true){
localStorage.clear();
update();
}
}