-
Notifications
You must be signed in to change notification settings - Fork 1
/
Note.html
157 lines (111 loc) · 4.93 KB
/
Note.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Notes App</title>
<link rel="stylesheet" href="Note.css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans&display=swap" rel="stylesheet">
<script type="text/javascript">
/*
*
* Refresh the items in the select
*
* Loads the items saved in the local storage
*
* LocalStorage allows to save key/value pairs in a web browser.
*
* @see <a href="https://www.w3schools.com/jsref/prop_win_localstorage.asp">Local Storage</a>
*
*/
function refresh() {
clearFields();
noteList = document.getElementById("notesList");
var length = noteList.options.length;
for (i = 0; i < length; i++) {
noteList.options[i] = null;
}
for (var i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).startsWith("Note")) {
var optionValue = localStorage.key(i).replace("Note_", '');
noteList.options[noteList.options.length] = new Option(optionValue.toUpperCase(), localStorage.key(i));
}
}
}
function addNote() {
var noteTitle = document.getElementById("noteTitle");
noteTitle.focus();
refresh();
clearFields();
}
/*
*
* Save the note to localstorage and refresh the view
*/
function saveNote() {
var noteTextArea = document.getElementById("noteBody");
var noteTitle = document.getElementById("noteTitle");
if (noteTitle.value !== null && noteTitle.value !== '' && noteTextArea.value !== null && noteTextArea.value !== '') {
localStorage.setItem("Note_" + noteTitle.value, noteTextArea.value);
alert("Note: " + noteTitle.value + " Saved");
clearFields();
refresh();
}
}
function clearFields() {
var noteTextArea = document.getElementById("noteBody");
var noteTitle = document.getElementById("noteTitle");
noteTextArea.value = "";
noteTitle.value = "";
}
/*
*
* Delete note from localstorage and refresh the view
*/
function deleteNote() {
if (document.getElementById("notesList").value !== null && document.getElementById("notesList").value !== '') {
if (confirm("Delete note: " + document.getElementById("notesList").value.replace("Note_", '') + " ?")) {
localStorage.removeItem(document.getElementById("notesList").value);
alert("Note deleted");
clearFields();
refresh();
}
}
}
function noteItemClick() {
noteList = document.getElementById("notesList");
noteText = localStorage.getItem((noteList.value));
var noteTextArea = document.getElementById("noteBody");
var noteTitle = document.getElementById("noteTitle");
noteTitle.value = noteList.value.replace("Note_", '').toUpperCase();//localStorage.key(noteList.value).replace("Note_",'');
noteTextArea.value = noteText;
}
</script>
</head>
<body>
<div id="noteShell">
<h1 id="notesName">Note Book</h1>
<a href="#" onclick="refresh()">Refresh</a>
<div id="readNote">
<button type="button" id="addButton" onclick="addNote()">Add Notes</button>
<select id="notesList" size="5" onclick="noteItemClick()" >
</select>
</div>
<div id="writeNote">
<div>
<label for="noteTitle"><b>Note Title</b></label>
</div>
<div>
<input type="text" id="noteTitle">
</div>
<div>
<label for="noteBody"><b>Notes</b></label>
</div>
<div>
<textarea id="noteBody" name="noteArea" rows="8" cols="80" placeholder="Write note here..."></textarea>
</div>
<button type="button" id="saveButton" onclick="saveNote()">Save Note</button>
<button type="button" id="deleteButton" onclick="deleteNote()">Delete Note</button>
</div>
</div>
</body>
</html>