-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteItDown.js
More file actions
119 lines (104 loc) · 3.69 KB
/
NoteItDown.js
File metadata and controls
119 lines (104 loc) · 3.69 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// console.log("Welcome to Note Making");
display(); //For reloading solution
// To display the notes
function display() {
let noteField = localStorage.getItem("data"); //getting the localStrorage items (strings)
if (noteField == null) {
noteArray = [];
//noteArray = null;
} else {
noteArray = JSON.parse(noteField); //convert string to array using JSON.parse
}
let htmlDisplay = "";
//Template literals used; element being the data in the array, index being its index no.
//<h4 class="head-note-section">Note ${index + 1}</h4>
//including title and note as a single object
noteArray.forEach(function (element, index) {
htmlDisplay =
htmlDisplay +
`<div class="saved-note-section">
<div class="inside-save-section">
<h4 class="head-note-section">${element.title}</h4>
<p class="para-note-section">${element.note}</p>
<h6 class="date-section">${element.idate}</h6>
</div>
<button class="delete-button" onclick="deleteNote(${index})">Delete</button>
</div> `;
});
let notesDisplay = document.querySelector(".saved-notes");
if (noteArray.length != 0) {
notesDisplay.innerHTML = htmlDisplay;
//console.log("Displaying saved notes");
} else {
notesDisplay.innerHTML = `No notes : Add something`;
//console.log("Displayed 0 notes");
}
}
//To add a note
let addNote = document.querySelector(".add-button");
addNote.addEventListener("click", function (e) {
//console.log("Let's add note");
let textField = document.querySelector(".textArea"); //select the textarea
let textTitle = document.querySelector(".addTitle");
let noteField = localStorage.getItem("data");
if (noteField == null) {
noteArray = [];
} else {
noteArray = JSON.parse(noteField);
}
//including title and note as a single object
let notesObject = {
title: textTitle.value,
note: textField.value,
idate: new Date().toUTCString()
};
if(notesObject.title==='' || notesObject.note===""){
console.log("Add a note")
}else{
noteArray.push(notesObject);
}
localStorage.setItem("data", JSON.stringify(noteArray));
textField.value = "";
textTitle.value="";
//console.log("Note Added to local storage");
//console.log(noteArray);
display(); //Display the note
});
//To delete the note
function deleteNote(index) {
//console.log("Deleting : ", index);
let noteField = localStorage.getItem("data");
if (noteField == null) {
noteArray = [];
} else {
noteArray = JSON.parse(noteField);
}
noteArray.splice(index, 1); //Splice function to remove elements in array
localStorage.setItem("data", JSON.stringify(noteArray));
//console.log("Deleted");
display();
}
//To search a note using keywords
let searchNote = document.querySelector(".search");
searchNote.addEventListener("input", function () {
//search the note using input
let searchText = searchNote.value;
console.log("Inputing :", searchText);
if (searchText.value == null) {
//It will display all the notes even if the input is null
display();
}
let searchButton = document.querySelector(".search-button"); //Using search button
searchButton.addEventListener("click", function () {
let textInSection = document.getElementsByClassName("saved-note-section");
Array.from(textInSection).forEach(function (element) {
let text = element.getElementsByTagName("p")[0].innerText; //selecting all the 1st paragraph elements
let titleText=element.getElementsByTagName("h4")[0].innerText;
if (text.includes(searchText) || titleText.includes(searchText)) {
element.style.display = "block";
} else {
element.style.display = "none"; //vanishes
}
});
});
});