forked from lsvekis/JavaScript-Exercises-Book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote Taking App
More file actions
52 lines (52 loc) · 1.43 KB
/
Note Taking App
File metadata and controls
52 lines (52 loc) · 1.43 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
<!-- Objective: Create a note-taking app that allows adding, viewing, and deleting notes. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Note Taking App</title>
</head>
<body>
<input type="text" id="noteInput" placeholder="Enter a note">
<button id="addNote">Add Note</button>
<ul id="noteList"></ul>
<script>
const addNoteButton = document.getElementById('addNote');
const noteInput = document.getElementById('noteInput');
const noteList = document.getElementById('noteList');
function saveNotes() {
const notes = [];
document.querySelectorAll('#noteList li').forEach(note => {
notes.push(note.textContent.replace('Delete', '').trim());
});
localStorage.setItem('notes', JSON.stringify(notes));
}
function loadNotes() {
const notes = JSON.parse(localStorage.getItem('notes')) || [];
notes.forEach(note => {
addNoteToList(note);
});
}
function addNoteToList(note) {
const li = document.createElement('li');
li.textContent = note + ' ';
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.addEventListener('click', () => {
noteList.removeChild(li);
saveNotes();
});
li.appendChild(deleteButton);
noteList.appendChild(li);
}
addNoteButton.addEventListener('click', () => {
const note = noteInput.value.trim();
if (note) {
addNoteToList(note);
saveNotes();
noteInput.value = ''; // Clear input after adding
}
});
loadNotes();
</script>
</body>
</html>