-
Notifications
You must be signed in to change notification settings - Fork 0
/
favourite.html
71 lines (58 loc) · 2.66 KB
/
favourite.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<title>Document</title>
</head>
<body class="bg-[url('https://wallpapercave.com/wp/wp4308762.jpg')] bg-cover bg-no-repeat">
<div class="grid grid-cols-1 gap-8 p-5 xl:grid-cols-5 lg:grid-cols-4 md:grid-cols-3 sm:grid-cols-2 transition-all transform ease-in-out float-none" id="results"></div>
</body>
</html>
<script>
// Get "fav" from local storage and parse the JSON string to an array. If there is no data in local storage, set an empty array.
let Favourite = JSON.parse(localStorage.getItem("fav")) || [];
// Log the "fav" array to the console.
console.log(Favourite);
// Get the "results" element from the DOM.
let results = document.getElementById("results");
// Call the "append" function and pass in the "fav" array.
append(Favourite);
// Define the "append" function, which takes in an array parameter named "Favourite".
function append(Favourite) {
// Clear the innerHTML of the "results" element.
results.innerHTML = null;
// Use the map method to iterate over each element in the "fav" array and create a new DOM element for each one.
Favourite.map((el, i) => {
// Create a new div element and set its class, using tailwind CSS classes.
let box = document.createElement("div");
box.setAttribute('class', 'h-auto pb-2 group shadow-lg hover:shadow-xl shadow-black rounded-md bg-slate-900 overflow-hidden hover:scale-105 text-white duration-500 transition-all ease-in-out');
let name = document.createElement("h3");
name.innerHTML = el.name;
name.setAttribute('class','text-center mt-3 text-md mx-2 font-bold')
let image = document.createElement("img");
image.setAttribute('class','p-3 rounded-[100%] h-40 w-40 block mx-auto')
image.src = el.thumbnail.path + "." + el.thumbnail.extension;
let deletes = document.createElement("button");
deletes.innerText = "Delete";
deletes.setAttribute('class','block mx-auto bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-4 rounded')
deletes.addEventListener("click", function () {
deleteFav(el.id);
});
box.append(name, image, deletes);
results.append(box);
});
}
// For deleting the hero from the favourite page
function deleteFav(id) {
console.log(id);
let updated = Favourite.filter((el, i) => {
return id !== el.id;
});
Favourite = updated;
localStorage.setItem("fav", JSON.stringify(updated));
append(updated);
}
</script>