-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_to_cart.js
57 lines (43 loc) · 1.57 KB
/
add_to_cart.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
let cartArr=JSON.parse(localStorage.getItem("cart"))||[];
let sum=0;
window.onload = function(){
document.querySelector("#shopcart").innerHTML="";
cartArr.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;
sum=sum+Number(el.price)
let remove=document.createElement("button");
remove.innerText="Remove"
remove.addEventListener("click", function(){
// console.log(el, index);
cartArr.splice(index, 1)
localStorage.setItem("cart", JSON.stringify(cartArr));
location.reload();
})
card.append(img, name, price, remove);
document.querySelector("#shopcart").append(card);
});
if(cartArr.length==0){
let error=document.createElement("h3");
error.innerText="Your shopping cart is Empty :("
document.querySelector("#shopcart").append(error);
}
document.querySelector("#totalcart").innerText=cartArr.length;
document.querySelector("#totalprice").innerText="$"+sum;
// proceed payment
let paymentData=[];
document.querySelector("#payment>button").addEventListener("click", function(){
let obj={
totprice:sum,
totitem:cartArr.length
}
paymentData.push(obj);
localStorage.setItem("paymentData", JSON.stringify(paymentData));
window.location.href="Payment.html"
})
}