-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (70 loc) · 2.33 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// console.log("krushna");
show();
let textValue = document.getElementById("textValue");
let addTask = document.getElementById("addTask");
addTask.addEventListener("click", function () {
let inputTextVal = textValue.value;
if (inputTextVal == "") alert("please Enter Task");
if (inputTextVal.trim() != 0) {
let todotask = localStorage.getItem("todo");
if (todotask == null) {
taskObj = [];
} else {
taskObj = JSON.parse(todotask);
}
taskObj.push(inputTextVal);
localStorage.setItem("todo", JSON.stringify(taskObj));
textValue.value = "";
}
show();
});
function show() {
let todotask = localStorage.getItem("todo");
if (todotask == null) {
taskObj = [];
} else {
taskObj = JSON.parse(todotask);
}
let html = "";
let taskList = document.getElementById("taskList");
taskObj.forEach((item, index) => {
html += ` <tr>
<th scope="row">${index + 1}.</th>
<td>${item}</td>
<td><button type="button" class="editBtn" onclick="edittask(${index})">Edit</button></td>
<td><button class="deleteBtn" onclick="deletetask(${index})">Delete</button></td>
</tr>`;
});
taskList.innerHTML = html;
}
function edittask(index) {
let saveindex = document.getElementById("saveindex");
let saveTask = document.getElementById("saveTask");
let addTask = document.getElementById("addTask");
saveindex.value = index;
let todotask = localStorage.getItem("todo");
let taskObj = JSON.parse(todotask);
textValue.value = taskObj[index];
addTask.style.display = "none";
saveTask.style.display = "block";
}
let saveTask = document.getElementById("saveTask");
saveTask.addEventListener("click", function () {
let addTask = document.getElementById("addTask");
let todotask = localStorage.getItem("todo");
let taskObj = JSON.parse(todotask);
let saveindex = document.getElementById("saveindex").value;
taskObj[saveindex] = textValue.value;
saveTask.style.display = "none";
addTask.style.display = "block";
localStorage.setItem("todo", JSON.stringify(taskObj));
textValue.value = "";
show();
});
function deletetask(index) {
let todotask = localStorage.getItem("todo");
let taskObj = JSON.parse(todotask);
taskObj.splice(index, 1);
localStorage.setItem("todo", JSON.stringify(taskObj));
show();
}