-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscript.js
154 lines (125 loc) · 4.88 KB
/
script.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Image Filter Section */
const allFilterItems = document.querySelectorAll('.filter-item');
const allFilterBtns = document.querySelectorAll('.filter-btn');
window.addEventListener('DOMContentLoaded', () => {
allFilterBtns[1].classList.add('active-btn');
});
allFilterBtns.forEach((btn) => {
btn.addEventListener('click', () => {
showFilteredContent(btn);
});
});
function showFilteredContent(btn){
allFilterItems.forEach((item) => {
if(item.classList.contains(btn.id)){
resetActiveBtn();
btn.classList.add('active-btn');
item.style.display = "block";
} else {
item.style.display = "none";
}
});
}
function resetActiveBtn(){
allFilterBtns.forEach((btn) => {
btn.classList.remove('active-btn');
});
}
/* Shopping Cart Section */
if (document.readyState == 'loading'){
document.addEventListener('DOMContentLoaded' , ready);
}
else{
ready();
}
function ready(){
var removeCartItemButton = document.getElementsByClassName('btn-danger');
for (var i = 0 ; i < removeCartItemButton.length; i++){
var button = removeCartItemButton[i];
button.addEventListener('click', removeCartItem)
}
var quantityInputs = document.getElementsByClassName('cart-quantity-input');
for(var i = 0 ;i < quantityInputs.length ; i++){
var input = quantityInputs[i];
input.addEventListener('change', quantityChanged);
}
var addToCartButtons = document.getElementsByClassName('shop-item-button');
for(var i = 0; i< addToCartButtons.length; i++){
var button = addToCartButtons[i];
button.addEventListener('click',addToCartClicked)
}
document.getElementsByClassName('btn-purchase')[0].addEventListener('click', purchaseClicked)
}
function purchaseClicked(){
alert('Thank you for your purchase!!!');
var cartItems = document.getElementsByClassName('cart-items')[0];
while(cartItems.hasChildNodes()){
cartItems.removeChild(cartItems.firstChild)
}
updateCartTotal();
}
function removeCartItem(event){
var buttonClicked = event.target;
buttonClicked.parentElement.parentElement.remove();
updateCartTotal();
}
function quantityChanged(event){
var input = event.target;
if(isNaN(input.value) || input.value <= 0 ){
input.value = 1;
}
updateCartTotal();
}
function addToCartClicked(event){
var button = event.target;
var shopItem = button.parentElement.parentElement;
var title = shopItem.getElementsByClassName('shop-item-title')[0].innerText;
var price = shopItem.getElementsByClassName('shop-item-price')[0].innerText;
var imageSrc = shopItem.getElementsByClassName('shop-item-image')[0].src;
addItemToCart(title,price,imageSrc);
updateCartTotal();
}
function addItemToCart(title, price, imageSrc){
var cartRow = document.createElement('tr');
cartRow.classList.add('cart-row');
var cartItems = document.getElementsByClassName('cart-items')[0];
var cartItemNames = cartItems.getElementsByClassName('cart-item-title');
for (i = 0; i< cartItemNames.length ; i++){
if(cartItemNames[i].innerText == title){
alert('This item already has added to the cart!');
return
}
}
var cartRowContents = `
<td class="cart-item cart-column">
<img class="cart-item-image" src="${imageSrc}" width="50" height="50">
<span class="cart-item-title">${title}</span>
</td>
<td class="cart-item cart-column">
<span class="cart-price cart-column">${price}</span>
</td>
<td class="cart-item cart-column">
<input class="cart-quantity-input" type="number" value="1" style="width: 50px">
<button class="btn btn-danger" type="button">Remove</button>
</td>
`;
cartRow.innerHTML = cartRowContents;
cartItems.append(cartRow);
cartRow.getElementsByClassName('btn-danger')[0].addEventListener('click', removeCartItem);
cartRow.getElementsByClassName('cart-quantity-input')[0].addEventListener('change', quantityChanged)
}
function updateCartTotal(){
var cartItemContainer = document.getElementsByClassName('cart-items')[0];
var cartRows = cartItemContainer.getElementsByClassName('cart-row');
var total = 0;
for (var i = 0 ; i< cartRows.length ; i++){
var cartRow =cartRows[i];
var priceElement = cartRow.getElementsByClassName('cart-price')[0];
var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0];
var price = parseFloat(priceElement.innerText.replace('Rs ' , ''))
var quantity = quantityElement.value;
total = total + (price * quantity);
}
total = Math.round(total * 100 )/100;
document.getElementsByClassName('cart-total-price')[0].innerText = 'Rs '+ total + '.00';
}