-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (85 loc) · 3.21 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
/** References:
* 1. https://www.youtube.com/watch?v=YeFzkC2awTM&t=863s
*
*/
if (document.readyState == "loading") {
document.addEventListener("DOMContentLoaded", ready);
} else {
ready();
}
function ready() {
// // var removeBagItems = document.getElementsByClassName("btn-danger");
// //console.log(removeBagItems);
// for (var i = 0; i < removeBagItems.length; i++) {
// var removeButton = removeBagItems[i];
// removeButton.addEventListener("click", removeBagItem);
// }
var quantityInputs = document.getElementsByClassName("bag-quantity-input");
for (var i = 0; i < quantityInputs.length; i++) {
var input = quantityInputs[i]
input.addEventListener('change', quantityChanged)
}
var addToBag = document.getElementsByClassName("btn btn-success");
for (var i = 0; i < addToBag.length; i++) {
var addToBagButton = addToBag[i];
addToBagButton.addEventListener('click', addToBagClicked);
}
}
function addToBagClicked(event){
var button = event.target
var buttonParent = button.parentElement
//console.log(button.parentElement);
var title = buttonParent.getElementsByClassName("panel-heading")[0].innerText;
var price = buttonParent.getElementsByClassName("list-item-price")[0].innerText;
var imageSource = buttonParent.getElementsByClassName("list-item-image")[0].src;
console.log(title,price,imageSource);
addItemToBag(title,price,imageSource);
}
function quantityChanged(event){
var input = event.target;
if (isNaN(input.value) || input.value <= -1){
input.value = 0;
}
updateBagTotal();
}
function removeBagItem(event){
//console.log('clicked');
var clickedButton = event.target;
clickedButton.parentElement.parentElement.remove();
updateBagTotal();
}
updateBagTotal();
function updateBagTotal() {
//var bagItemContainer = document.getElementsByClassName("bag-table")[0];
//console.log(bagItemContainer);
//var bagRows = bagItemContainer.getElementsByClassName("bag-item-row");
var bagRowPrices = document.getElementsByClassName("bag-item-price");
var bagRowQuantity = document.getElementsByClassName("bag-quantity-input");
var totalSum = 0;
for (var i = 0; i < bagRowPrices.length; i++) {
var price = parseFloat(bagRowPrices[i].innerText.replace("$",""));
var quantity_1 = bagRowQuantity[i].value;
totalSum = totalSum + price * quantity_1;
console.log(price * quantity_1);
//console.log(bagRowPrices[i].innerText);
//console.log(bagRowQuantity[i].value);
}
totalSum = Math.round(totalSum * 100) / 100 ;
document.getElementsByClassName("bag-total-price")[0].innerText =
"$" + totalSum;
// for (var i=0; i < bagRows.length ; i++){
// var bagRowItems = bagRows[i];
// //alert(bagRows);
// var price = bagRowItems.getElementsByClassName("bag-item-price")[0];
// var quantity = bagRowItems.getElementsByClassName("bag-quantity-input")[0];
// alert(price,quantity);
// }
}
function purchaseFunction(){
// alert("Thank you for Donating!");
var bagRowQuantity = document.getElementsByClassName("bag-quantity-input");
for (var i = 0; i < bagRowQuantity.length; i++) {
bagRowQuantity[i].value = 0;
}
setTimeout("location.reload(true)",3000);
}