-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
56 lines (50 loc) · 1.7 KB
/
main.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
function addTodoList() {
if (
document.getElementById("title").value != "" &&
document.getElementById("description").value != ""
) {
let myArr = [];
let title = document.getElementById("title").value;
let descript = document.getElementById("description").value;
if (localStorage.getItem("itemJson") == null) {
myArr.push([title, descript]);
localStorage.setItem("itemJson", JSON.stringify(myArr));
} else {
myArr = JSON.parse(localStorage.getItem("itemJson"));
myArr.push([title, descript]);
localStorage.setItem("itemJson", JSON.stringify(myArr));
}
//populating the todo's table
updateTodoList();
}
}
function updateTodoList() {
myArr = JSON.parse(localStorage.getItem("itemJson"));
let myBody = document.getElementById("tableBody");
let str = "";
myArr.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 btn-sm" onclick="deleteTask(${index})">Delete</button></td>
</tr>`;
});
myBody.innerHTML = str;
}
function deleteTask(myIndex) {
console.log("inside delete finction");
let myTasks = JSON.parse(localStorage.getItem("itemJson"));
myTasks.splice(myIndex, 1);
localStorage.setItem("itemJson", JSON.stringify(myTasks));
// location.reload();
updateTodoList();
}
//updating document, on start up
if (localStorage.getItem("itemJson") != null) {
updateTodoList();
}
//adding todo's in my todo-list table
let add = document.getElementById("add");
add.addEventListener("click", addTodoList);