-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
102 lines (86 loc) · 2.66 KB
/
app.js
File metadata and controls
102 lines (86 loc) · 2.66 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
let addBtn = document.getElementById("add-btn");
let addTitle = document.getElementById("note-title")
let addTxt = document.getElementById("note-text")
addBtn.addEventListener("click", (e) =>{
if (addTitle.value == "" || addTxt.value == "") {
return alert("Please add note title and details");
}
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
let myObj = {
title: addTitle.value,
text: addTxt.value
}
notesObj.push(myObj);
localStorage.setItem("notes", JSON.stringify(notesObj));
addTitle.value = "";
addTxt.value = "";
showNotes();
})
// show notes on the page
function showNotes() {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function(element, index){
html += `
<div id="note">
<p class="note-counter">Note ${index + 1}</p>
<h3 class="note-title">${element.title}</h3>
<p class="note-text">${element.text}</p>
<button id="${index}" onclick="deleteNote(this.id)" class="note-btn">Delete Note</button>
<button id="${index}" onclick="editNote(this.id)" class="note-btn edit-btn">Edit Note</button>
</div>
`;
});
let noteElm = document.getElementById("notes");
if(notesObj.length != 0) {
noteElm.innerHTML = html;
} else{
noteElm.innerHTML = "No notes yet! Add a note using the form above.";
}
}
// Function to delete note
function deleteNote(index){
let confirmDel = confirm("You are deleting this note!!");
if (confirmDel == true) {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
}
}
//Function to edit note
function editNote(index){
let notes = localStorage.getItem("notes");
if (addTitle.value !== "" || addTxt.value !== ""){
return alert("Please clear the form before editing the note");
}
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
//console.log(notesObj);
notesObj.findIndex((element, index) => {
addTitle.value = element.title;
addTxt.value = element.text;
})
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
}
showNotes();