forked from Tzikas/ironstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (66 loc) · 2.34 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
//Variables, Arrays, and Objects, dotNotation, bracketNotation
//Dom manipulation
let items = [
{
name: 'Ironhack T',
price: 10,
image: 'https://miro.medium.com/max/5190/1*[email protected]'
},
{
name: 'Ironhack Hoodie',
price: 15,
image: 'https://m.media-amazon.com/images/I/B1i3u9-Q-KS._AC_CLa%7C2140%2C2000%7CB1wqstnnTfS.png%7C0%2C0%2C2140%2C2000%2B0.0%2C0.0%2C2140.0%2C2000.0_UL1500_.png'
},
{
name: 'Ironhack Sticker',
price: 2,
image:'https://e7.pngegg.com/pngimages/887/803/png-clipart-ironhack-web-development-job-startup-company-design-blue-user-interface-design-thumbnail.png'
},
{
name: 'Ironhack Mug',
price: 8,
image: 'https://d0bb7f9bf11b5ad1a6b2-6175f06f5e3f64e15abbf67415a276ec.ssl.cf1.rackcdn.com/product-images/designlab/11-oz-traditional-ceramic-coffee-mugs-7102-white1582888132.jpg'
},
]
let cart = []
let list = document.querySelector('#items')
items.forEach((item,i)=>{
list.innerHTML += `<li>
<div>Name: ${item.name}</div>
<div>price: $${item.price}</div>
<image src="${item.image}" />
<input type="number" placeholder="quantity" onchange='inputChange(${i}, "${item.name}", "${item.price}", "${item.image}")'/>
<button>Buy Item</button>
</li>`
})
function showCart() {
let cartItems = document.querySelector('#cart')
let grandTotal = 0;
cartItems.innerHTML = ''
cart.forEach((item,i)=>{
grandTotal+= item.price * item.quantity
cartItems.innerHTML += `<li>
<div>Name: ${item.name}</div>
<div>Quantity: ${item.quantity}</div>
<image src="${item.image}" />
</li>`
})
document.querySelector('#grandTotal').innerHTML = '$' +grandTotal
}
function inputChange(i, name, price,image) {
console.log('I want to buy the ',i,' item named, ',name, ' that costs $',price)
let listItem = document.querySelectorAll('li')[i]
let input = listItem.querySelector('input')
let button = listItem.querySelector('button')
button.onclick = function(){
cart.push({
quantity: input.value,
name: name,
price: price,
image: image
})
console.log(cart)
showCart()
}
}
//document.querySelector('#two').style.backgroundColor = 'blue'