-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (71 loc) · 2.18 KB
/
app.js
File metadata and controls
86 lines (71 loc) · 2.18 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
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
77
78
79
80
81
82
83
84
85
86
var home_container = document.getElementById("home_container");
var note = document.getElementById("note");
function submitNote() {
var timeupdate = new Date().toLocaleString();
var obj = {
note: note.value,
time: timeupdate
};
saveValueToLocalStorage(obj);
note.value = "";
}
function saveValueToLocalStorage(obj) {
var notes = localStorage.getItem("notes");
console.log("notes from local storage=>", notes);
if (notes) {
notes = JSON.parse(notes);
notes.push(obj);
console.log(notes);
localStorage.setItem("notes", JSON.stringify(notes));
} else {
notes = [obj];
console.log(notes);
localStorage.setItem("notes", JSON.stringify(notes));
}
displayUserNotes();
}
function displayUserNotes() {
var notes = localStorage.getItem("notes");
var list = document.getElementById("list");
if (notes) {
list.innerHTML = "";
notes = JSON.parse(notes);
console.log(notes);
notes.forEach(function (data, ind) {
console.log("data=>", data);
var liElement =
` <li class="note-box">
<div class="note-content">
<p class="note-text">${data.note}</p>
</div>
<div class="button-group">
<p class="note-time">${data.time}</p>
<div class="btn-group">
<button onclick="editNote(${ind})" class="btn-edit">Edit</button>
<button onclick="deleteNote(${ind})" class="btn-delete">Delete</button>
</div>
</div>
</li>`;
list.innerHTML += liElement;
});
}
}
function editNote(index) {
var notes = JSON.parse(localStorage.getItem("notes"));
var currentNote = notes[index];
var newNoteText = prompt("Edit note:", currentNote.note);
if (newNoteText !== null) {
notes[index].note = newNoteText;
currentNote.time = new Date().toLocaleString();
localStorage.setItem("notes", JSON.stringify(notes));
displayUserNotes();
}
}
function deleteNote(index) {
var notes = JSON.parse(localStorage.getItem("notes"));
notes.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notes));
displayUserNotes();
}
// Initial call to display notes
displayUserNotes();