-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (81 loc) · 2.42 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
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
const input = document.getElementById("input");
const addBtn = document.querySelector(".addBtn");
const removeBtn = document.querySelector(".removeBtn");
const clearAllBtn = document.querySelector(".clearAllBtn");
const notesList = document.querySelector(".notesList");
const notesTitle = document.querySelector(".notesTitle");
//Creating an empty array to store all notes in localStorage
let notesArr;
//we will call this function to show our notes on screen
function showNotes() {
const allNotes = JSON.parse(localStorage.getItem("notes"));
if (allNotes == null) {
notesList.innerHTML = `
<div class="nullText">
No any notes to display. Please add notes.
</div>
`;
} else {
const note = allNotes
.map((e, index) => {
if (e.title.length == 0){
title = `Note ${index+1}`;
}else {
title = e.title;
}
return `
<div class="note">
<h5 class="note_title">${title}</h5>
<div class="note_content">${e.content}</div>
<button onClick="remove(${index})" class="removeBtn btn">Remove</button>
</div>
`;
})
.join(" ");
notesList.innerHTML = note;
}
}
showNotes();
//This functional will add current note to localstorage
const addNote = () => {
let inputText = {
title: notesTitle.value,
content: input.value
};
if (inputText.content.length == 0 ) {
alert("Please add some content to your notes!");
} else {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesArr = [];
} else {
notesArr = JSON.parse(notes);
}
notesArr.push(inputText);
localStorage.setItem("notes", JSON.stringify(notesArr));
notesTitle.value= "";
input.value = "";
}
showNotes();
};
//btn to add note
addBtn.addEventListener("click", addNote);
input.addEventListener("submit", addNote);
const clearAllNotes = () => {
if (confirm("Do you want to remove all your notes?")) {
localStorage.clear();
}
showNotes();
};
clearAllBtn.addEventListener("click", clearAllNotes);
const remove= (index)=> {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesArr = [];
} else {
notesArr = JSON.parse(notes);
}
notesArr.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesArr));
showNotes();
}