-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (38 loc) · 1.15 KB
/
script.js
File metadata and controls
43 lines (38 loc) · 1.15 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
let getValue = document.getElementById('inp');
let store = JSON.parse(localStorage.getItem('todo')) || [];
function rander(){
let list = document.getElementById('list');
list.innerHTML = "";
for (let i = 0; i < store.length; i++) {
list.innerHTML += `
<li>
<span>${store[i]}</span>
<button class="edit" onclick="editItem(${i})">Edit</button>
<button class="del" onclick="deleteItem(${i})">Del</button>
</li>`;
}
}
function add(){
if (getValue.value.trim() !== "") {
store.push(getValue.value);
localStorage.setItem('todo', JSON.stringify(store));
rander();
getValue.value = "";
} else {
alert("Input field is empty!");
}
}
function deleteItem(index) {
store.splice(index, 1);
localStorage.setItem('todo', JSON.stringify(store));
rander();
}
function editItem(index) {
let newValue = prompt("Edit your item:", store[index]);
if (newValue !== null && newValue.trim() !== "") {
store[index] = newValue;
localStorage.setItem('todo', JSON.stringify(store));
rander();
}
}
rander();