-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (54 loc) · 1.7 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
51
52
53
54
function addContent() {
let task_val = document.getElementById("task").value;
let description_val = document.getElementById("description").value;
if (localStorage.getItem("items") == null) {
items_arr = [];
items_arr.push([task_val, description_val]);
localStorage.setItem("items", JSON.stringify(items_arr));
} else {
items_str = localStorage.getItem("items");
items_arr = JSON.parse(items_str);
items_arr.push([task_val, description_val]);
localStorage.setItem("items", JSON.stringify(items_arr));
}
addToTable();
}
function addToTable() {
if (localStorage.getItem("items") == null) {
items_arr = [];
localStorage.setItem("items", JSON.stringify(items_arr));
} else {
items_str = localStorage.getItem("items");
items_arr = JSON.parse(items_str);
}
let table_content = document.getElementById("table-contents");
let str = "";
items_arr.forEach((element, idx) => {
str += `<tr>
<th scope="row">${idx + 1}</th>
<td>${element[0]}</td>
<td>${element[1]}</td>
<td><button class="btn btn-sm btn-primary" onclick="deleteContent(${idx})">Delete</button></td>
</tr>`;
});
table_content.innerHTML = str;
}
function deleteContent(item_idx) {
let items_str1 = localStorage.getItem("items");
let items_arr1 = JSON.parse(items_str1);
items_arr1.splice(item_idx, 1);
localStorage.setItem("items", JSON.stringify(items_arr1));
addToTable();
}
function clearlist() {
if (confirm("Are you sure?")) {
localStorage.clear();
addToTable();
}
}
function confirm_submit(){
confirm("Do you want to submit");
}
localStorage.clear();
addtolist = document.getElementById("add-to-list");
addtolist.addEventListener("click", addContent);