-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavourites.js
62 lines (48 loc) · 1.56 KB
/
favourites.js
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
let favouritebarList = document.getElementById('favlistitem');
let list = [];
list = JSON.parse(localStorage.getItem('favlistarr'));
// to fetch the updated list
function fetching(list) {
for (var i = 0; i < list.length; i++) {
loadhero(list[i]);
}
}
// loading data of hero function
async function loadhero(heroid) {
const URL = "https://www.superheroapi.com/api.php/3078862828893622/" + heroid.trim();
console.log(URL);
const res = await fetch(`${URL}`);
const data = await res.json();
console.log(data);
if (data){
herolistdis(data);
}
}
// to display the data of the movie
function herolistdis(hero) {
let herolistitem = document.createElement('div');
herolistitem.innerHTML = "";
herolistitem.innerHTML = `
<div id="outerbox">
<div id="innerbox">
<img src = "${hero.image.url}" id="favlistimg">
</div>
<H5>${hero.name}</H5>
<button class="btn btn-primary" id="remove" type="submit" onclick="remove(this.value)" value=${hero.id}>Remove</button>
</div>
`;
favouritebarList.appendChild(herolistitem);
}
// to remove the item from the list
function remove(value) {
for (let i = 0; i < list.length; i++) {
if (list[i] === value) {
list.splice(i, 1);
}
}
// console.log("remove running ")
localStorage.setItem('favlistarr', JSON.stringify(list));
favouritebarList.innerHTML = "";
fetching(list);
}
fetching(list);