|
| 1 | +const taxRate = 0.18; |
| 2 | +const shippingPrice = 15.0; |
| 3 | + |
| 4 | +window.addEventListener("load", () => { |
| 5 | + localStorage.setItem("taxRate", taxRate); |
| 6 | + localStorage.setItem("shippingPrice", shippingPrice); |
| 7 | + sessionStorage.setItem("taxRate", taxRate); |
| 8 | + sessionStorage.setItem("shippingPrice", shippingPrice); |
| 9 | + //add after func. coding |
| 10 | + calculateCartTotal() |
| 11 | +}); |
| 12 | + |
| 13 | +//capturing vs. bubbling |
| 14 | +let productsDiv = document.querySelector(".products"); |
| 15 | +productsDiv.addEventListener("click", (e) => { |
| 16 | + //console.log(event.target); |
| 17 | + //minus buttons |
| 18 | + if (e.target.classList.contains("minus")) { |
| 19 | + // console.log("minusBtn clicked"); |
| 20 | + if (e.target.nextElementSibling.innerText > 1) { |
| 21 | + // e.target.parentElement.lastElementChild.innerText; |
| 22 | + e.target.nextElementSibling.innerText--; |
| 23 | + //calculate Product and Cart Total |
| 24 | + //passing selectedProductInfo as parameter |
| 25 | + calculateProductAndCartTotal(e.target.parentElement.parentElement); |
| 26 | + } |
| 27 | + else { |
| 28 | + if (confirm("Product will be removed!")) { |
| 29 | + e.target.parentElement.parentElement.parentElement.remove(); |
| 30 | + //calculateCartTotal |
| 31 | + calculateCartTotal(); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + //plus buttons |
| 37 | + else if (e.target.className == "plus") { |
| 38 | + // console.log("plusBtn clicked"); |
| 39 | + // e.target.parentElement.firstElementChild.innerText; |
| 40 | + e.target.previousElementSibling.innerText++; |
| 41 | + //calculate Product and Cart Total |
| 42 | + calculateProductAndCartTotal(e.target.parentElement.parentElement); |
| 43 | + } |
| 44 | + |
| 45 | + //remove buttons |
| 46 | + else if (e.target.className == "remove-product") { |
| 47 | + // console.log("removeBtn clicked"); |
| 48 | + if (confirm("Product will be removed")) { |
| 49 | + e.target.parentElement.parentElement.parentElement.remove(); |
| 50 | + //calculateCartTotal |
| 51 | + calculateCartTotal() |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + //other elements |
| 56 | + else { |
| 57 | + console.log("other elements clicked"); |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | + |
0 commit comments