-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavourite.js
55 lines (39 loc) · 1.6 KB
/
favourite.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
let favArr=JSON.parse(localStorage.getItem("fav"))||[];
let cartArr=JSON.parse(localStorage.getItem("cart"))||[];
window.onload = function(){
document.querySelector("#favdiv").innerHTML="";
favArr.forEach(function (el, index){
let card= document.createElement("div")
let img=document.createElement("img");
img.src=el.img;
let name=document.createElement("h1");
name.innerText=el.name;
let price=document.createElement("h1");
price.innerText="Price: $"+el.price;
let addtocart=document.createElement("button");
addtocart.innerText="Add To Cart"
addtocart.addEventListener("click", function(){
cartArr.push(el);
localStorage.setItem("cart", JSON.stringify(cartArr));
favArr.splice(index, 1)
localStorage.setItem("fav", JSON.stringify(favArr));
location.reload();
})
let remove=document.createElement("button");
remove.innerText="Remove"
remove.addEventListener("click", function(){
// console.log(el, index);
favArr.splice(index, 1)
localStorage.setItem("fav", JSON.stringify(favArr));
location.reload();
})
card.append(img, name, price, addtocart, remove);
document.querySelector("#favdiv").append(card);
});
if(favArr.length==0){
let error=document.createElement("h3");
error.innerText="You haven't added any product to your Loves list."
document.querySelector("#favdiv").append(error);
}
document.querySelector("#totalfav").innerText=favArr.length
}