Repository navigation
Expand file tree
/
Copy pathaddtocart.js
More file actions
99 lines (91 loc) · 2.4 KB
/
Copy pathaddtocart.js
File metadata and controls
99 lines (91 loc) · 2.4 KB
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
const product = [
{
id: 0,
image:'image/boat.jpeg' ,
title: "Boat Netflix Edition HeadPhone",
price: 4999,
},
{
id: 1,
image: 'image/noise.jpeg',
title: "Noise Buds Combat",
price: 1499,
},
{
id: 2,
image:'image/bracelet.jpeg' ,
title: "Natural Healing Stone Bracelet",
price: 999,
},
{
id: 3,
image:'image/goldb.jpeg' ,
title: "Gold Bracelet",
price: 8799,
},
{
id: 4,
image:'image/shoe.jpeg' ,
title: "Nike Shoe",
price: 8049,
},
{
id: 5,
image:'image/campusshoe.jpeg' ,
title: "Campus Shoe",
price: 1289,
},
];
const categories = [...new Set(product.map((item)=>
{return item}))]
let i=0;
document.getElementById('root').innerHTML = categories.map((item)=>
{
var {image, title, price} = item;
return(
`<div class="box">
<div class="img-box">
<img class='images' src=${image}></img>
</div>
<div class=bottom>
<p>${title}</p>
<h2>₹ ${price}.00</h2>`+
"<button onclick='addtocart("+(i++)+")'> Add to Cart </button>"+
`</div>
</div>`
)
}).join('')
var cart = [];
function addtocart(a){
cart.push({...categories[a]});
displaycart();
}
function delElement(a){
cart.splice(a, 1);
displaycart();
}
function displaycart(a){
let j=0, total=0;
document.getElementById("count").innerHTML = cart.length;
if(cart.length==0){
document.getElementById('cartItem').innerHTML = "Your Cart is Empty !!";
document.getElementById("total").innerHTML = "₹ "+0+".00";
}
else{
document.getElementById("cartItem").innerHTML = cart.map((items)=>
{
var {image, title, price} = items;
total=total+price;
document.getElementById('total').innerHTML="₹ "+total+".00";
return(
`<div class='cart-item'>
<div class='row-img'>
<img class='rowing' src=${image}>
</div>
<p style='font-size:12px;'>₹ ${title}</p>
<h2 style='font-size:15px;'>₹ ${price}.00</h2>`+
"<i class='fa-solid fa-trash' onclick='delElement("+ (j++) +")'></i></div>"
);
}).join('')
}
}