-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveForLater.html
117 lines (99 loc) · 4.68 KB
/
saveForLater.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Arnold Twala">
<script src="https://kit.fontawesome.com/724c68c0fa.js" crossorigin="anonymous"></script>
<!-- google fonts links -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Merriweather&family=Montserrat&family=Sacramento&display=swap"
rel="stylesheet">
<!-- icon for the webpage Tab -->
<link rel="icon" type="image/png" href="./images/logo.png" sizes="48x48">
<link rel="stylesheet" href="style.css">
<title>Save for Later</title>
</head>
<body onload="displaySavedItems()">
<!-- this is the heading, and navigation menu for all three pages -->
<header id="top">
<div class="logo">
<img src="./images/logo.png" alt="Logo" width="55" height="55">
<h1><a href="index.html">Shaped by the Pages</a></h1>
</div>
<nav>
<ul class="nav-menu">
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="contacts.html">Contacts</a></li>
<li><a href="saveForLater.html">Save for Later</a></li>
</ul>
</nav>
</header>
<!-- this is the section for the save for later page -->
<main>
<h1>Items Saved for Later</h1>
<p>Here are the items you've saved for later:</p>
<div id="savedItemsContainer">
<!-- this is where the saved items will be displayed -->
</div>
</main>
<!-- A link to that takes you back to the top of the page -->
<a href="#top">Back to top</a>
<!-- A footer that contains name and the date the website was created, all three pages uses the same footer -->
<script>
function displaySavedItems() {
// get the saved items from local storage
let savedItems = getSaveForLaterItems();
// get the container where the saved items will be displayed
let savedItemsContainer = document.getElementById("savedItemsContainer");
// Clear the saved items container
savedItemsContainer.innerHTML = '';
// Add each saved item to the div
savedItems.forEach(item => {
// Create a new div for the item and its delete button
let itemDiv = document.createElement('div');
// If the content is an HTML element, append it directly
if (item.content instanceof HTMLElement) {
itemDiv.appendChild(item.content);
} else {
// If the content is not an HTML element (for example, if it's a string),
// create a new text node with the content and append that
let textNode = document.createTextNode(item.content);
itemDiv.appendChild(textNode);
}
// Create a delete button
let deleteButton = document.createElement('button');
deleteButton.className = 'deleteButton'; // Add a CSS class to the deleteButton
deleteButton.innerText = "Delete";
deleteButton.onclick = function () {
deleteSavedItem(item.id)
};
// Append the delete button to the item div
itemDiv.appendChild(deleteButton);
// Append the item div to the saved items container
savedItemsContainer.appendChild(itemDiv);
})
}
//
function deleteSavedItem(itemId) {
let saveForLaterItems = JSON.parse(localStorage.getItem('saveForLater'));
// Filter out the item with the given ID
saveForLaterItems = saveForLaterItems.filter(item => item.id !== itemId);
// Save back to localStoragex
localStorage.setItem('saveForLater', JSON.stringify(saveForLaterItems));
// Alert the user that the item has been deleted
alert("Item deleted. You now have " + saveForLaterItems.length + " item(s) in your folder.");
// Re-display the saved items
displaySavedItems();
}
</script>
<footer>
<p>A passion for reading, a gift for book lovers.</p>
<p>Created by: <a href="https://www.linkedin.com/in/arnoldtwl/">Arnold Twala</a></p>
<p>© 2023 Shaped by the Pages. All rights reserved.</p>
</footer>
<script src="./scripts/saveForLater.js"></script>
</body>
</html>