-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (70 loc) · 2.65 KB
/
app.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
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
//show cart
(function() {
const cartInfo = document.getElementById("cart-info");
const cart = document.getElementById("cart");
cartInfo.addEventListener("click", function() {
cart.classList.toggle("show-cart");
});
})();
//add items to the cart
(function() {
const cartBtn = document.querySelectorAll
(".store-item-icon");
cartBtn.forEach(function(btn) {
btn.addEventListener("click", function(event) {
if(event.target.parentElement.classList.contains("store-item-icon"))
("store-item-icon")) {
let fullPath =
event.target.parentElement.previousElementSibling.src;
let pos = fullPath.indexof("img") + 3;
let partPath = fullPath.slice(pos);
const item={};
item.img = 'web${partPath}';
let name =
event.target.parentElement.parentElement.nextElementSibling
.children[0].children[0].textContent;
item.name=name;
let price=
event.target.parentElement.parentElement.nextElementSibling
.children[0].children[1].textContent;
let finalPrice = price.slice(1).trim();
item.price = finalPrice;
const cartItem=document.createElement('div');
cartItem.classList.add("cart-item","d-flex","justify-content-between","text-capitalize","my-3");
cartItem.innerHTML = '
<img src="${item.img}" class="img-fluid rounded-circle" id="item-img" alt="">
<div class="item-text">
<p id="cart-item-title" class="font-weight-bold mb-0">${item.name}</p>
<span>$</span>
<span id="cart-item-price" class="cart-item-price" class="mb-0">${item.price}</span>
</div>
<a href="#" id='cart-item-remove' class="cart-item-remove">
<i class="fas fa-trash"></i>
</a>
</div>
';
//select cart
const cart = document.getElementById("cart");
const total = document.querySelector(".cart-total-container");
cart.insertBefore(cartItem, total);
alert("item added to the cart");
showTotals();
}
});
});
//show totals
function showTotals() {
const total = [];
const items = document.querySelectorAll(".cart-item-price");
items.forEach(function(item) {
total.push(parseFloat(item.textContext));
});
const totalMoney = toal.reduce(function(total, item) {
total+=item;
return total;
}, 0);
const finalMoney = totalMoney.toFixed(2);
document.getElementById("cart-total").textContent = finalMoney;
document.querySelector(".item-total").textContent = finalMoney;
document.getElementById("item-count").textContent = total.length;
})();