-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (59 loc) · 2.06 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
function getAndUpdate(){
task = document.getElementById('inpNewTask').value;
desc = document.getElementById('description').value;
if (localStorage.getItem('itemsJson')==null){
itemJsonArray = [];
itemJsonArray.push([task, desc]);
localStorage.setItem('itemsJson', JSON.stringify(itemJsonArray))
}
else{
itemJsonArrayStr = localStorage.getItem('itemsJson')
itemJsonArray = JSON.parse(itemJsonArrayStr);
itemJsonArray.push([task, desc]);
localStorage.setItem('itemsJson', JSON.stringify(itemJsonArray))
}
update();
}
function update(){
if (localStorage.getItem('itemsJson') == null){
itemJsonArray = [];
localStorage.setItem('itemsJson', JSON.stringify(itemJsonArray))
}
else{
itemJsonArrayStr = localStorage.getItem('itemsJson')
itemJsonArray = JSON.parse(itemJsonArrayStr);
}
let tableBody = document.getElementById("tableBody");
let 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-sm btn-primary" onclick="deleted(${index})">Delete</button></td>
</tr>`;
});
tableBody.innerHTML = str;
}
btnAdd = document.getElementById("btnAdd");
btnAdd.addEventListener("click", getAndUpdate);
update();
function deleted(itemIndex){
console.log("Delete", itemIndex);
itemJsonArrayStr = localStorage.getItem('itemsJson')
itemJsonArray = JSON.parse(itemJsonArrayStr);
itemJsonArray.splice(itemIndex, 1);
localStorage.setItem('itemsJson', JSON.stringify(itemJsonArray));
update();
}
function clearAll(){
if (confirm("Do you areally want to clear?")){
console.log('Clearing the storage')
localStorage.clear();
update()
document.getElementById('tableBody').innerHTML = "No list to show";
tableBody.style.fontWeight = 'bold';
tableBody.style.textAlign = 'center';
}
}