-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.html
397 lines (342 loc) · 16.2 KB
/
list.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<style>
.fit {
height: 400px;
width: 600px;
background-size: cover;
}
.sidebar {
float: left;
}
.product {
width: 500px;
height: 400px;
}
.col {
padding: 2rem;
}
.cart {
width: 50px;
height: 50px;
}
</style>
<title>Shopping Cart</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="https://shreya024.github.io/JSProject/">Projects</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="jsclock.html">jsclock</a>
</li>
<li class="nav-item">
<a class="nav-link" href="weatherapp.html">Weather</a>
</li>
<li class="nav-item">
<a class="nav-link" href="list.html">Shopping List</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Other Projects
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="anicar.html">Animated Car</a></li>
<li><a class="dropdown-item" href="animgame.html">Game</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<div id="carouselExampleCaptions" class="carousel slide right" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="0" class="active"
aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="1"
aria-label="Slide 2"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="2"
aria-label="Slide 3"></button>
</div>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="home.jpg" class="d-block w-100 fit" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Home</h5>
<p>Welcome to myStore.</p>
</div>
</div>
<div class="carousel-item">
<img src="store.jpg" class="d-block w-100 fit" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>Store</h5>
<p>A glance at our new arrivals.</p>
</div>
</div>
<div class="carousel-item">
<img src="aboutus.jpg" class="d-block w-100 fit" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>About</h5>
<p>A sneak peek about us.</p>
</div>
</div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleCaptions"
data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleCaptions"
data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
<div class="px-4 pt-5 my-5 text-center border-bottom">
<h1 class="display-4 fw-bold">Headphones</h1>
<div class="col-lg-6 mx-auto">
<div class="overflow-hidden">
<div class="container px-5 ">
<img src="product1.jpg" class="img-fluid border rounded-3 shadow-lg mb-4 product"
alt="Example image" width="700" height="500" loading="lazy">
</div>
</div>
</div>
<div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5">
<p class="btn btn-lg px-4 border rounded-3 price">$0.06</p>
<button type="button" class="btn btn-primary btn-lg px-2 addtocart">Add to Cart</button>
</div>
</div>
<div class="px-4 pt-5 my-5 text-center border-bottom">
<h1 class="display-4 fw-bold">Latest iPhone</h1>
<div class="col-lg-6 mx-auto">
<div class="overflow-hidden">
<div class="container px-5 ">
<img src="product2.png" class="img-fluid border rounded-3 shadow-lg mb-4 product"
alt="Example image" width="700" height="500" loading="lazy">
</div>
</div>
</div>
<div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5">
<p class="btn btn-lg px-4 border rounded-3 price">$0.06</p>
<button type="button" class="btn btn-primary btn-lg px-2 addtocart">Add to Cart</button>
</div>
</div>
<div class="px-4 pt-5 my-5 text-center border-bottom">
<h1 class="display-4 fw-bold">Fitbit</h1>
<div class="col-lg-6 mx-auto">
<div class="overflow-hidden">
<div class="container px-5 ">
<img src="product3.png" class="img-fluid border rounded-3 shadow-lg mb-4 product"
alt="Example image" width="700" height="500" loading="lazy">
</div>
</div>
</div>
<div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5">
<p class="btn btn-lg px-4 border rounded-3 price">$0.06</p>
<button type="button" class="btn btn-primary btn-lg px-2 addtocart">Add to Cart</button>
</div>
</div>
<div class="px-4 pt-5 my-5 text-center border-bottom">
<h1 class="display-4 fw-bold">french Fries</h1>
<div class="col-lg-6 mx-auto">
<div class="overflow-hidden">
<div class="container px-5 ">
<img src="product4.png" class="img-fluid border rounded-3 shadow-lg mb-4 product"
alt="Example image" width="700" height="500" loading="lazy">
</div>
</div>
</div>
<div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5">
<p class="btn btn-lg px-4 border rounded-3 price">$0.06</p>
<button type="button" class="btn btn-primary btn-lg px-2 addtocart">Add to Cart</button>
</div>
</div>
<div class="px-4 pt-5 my-5 text-center border-bottom">
<h1 class="display-4 fw-bold">Sneakers</h1>
<div class="col-lg-6 mx-auto">
<div class="overflow-hidden">
<div class="container px-5 ">
<img src="product5.jpg" class="img-fluid border rounded-3 shadow-lg mb-4 product"
alt="Example image" width="700" height="500" loading="lazy">
</div>
</div>
</div>
<div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5">
<p class="btn btn-lg px-4 border rounded-3 price">$0.06</p>
<button type="button" class="btn btn-primary btn-lg px-2 addtocart">Add to Cart</button>
</div>
</div>
<div class="container my-10 ">
<div class="container my-10 mx-auto text-center ">
<h2>Your Cart</h2>
<table class="table table-striped w-50 mx-auto">
<thead>
<tr>
<th scope="col">ItemImg</th>
<th scope="col">Item</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="cartitems">
</div>
</div>
<h1 class="text-right" id="total">Total: </h1>
<div class="text-center">
<button class="btn btn-primary btn-lg btn-purchase" onclick="purchaseClicked()">Purchase</button>
</div>
<br><br>
<footer class="pt-4 my-md-5 pt-md-5 border-top">
<div class="row">
<div class="col-12 col-md">
<img class="mb-2" src="..." alt="" width="24" height="19">
<small class="d-block mb-3 text-muted">© 2017–2021</small>
</div>
<div class="col-6 col-md">
<h5>Features</h5>
</div>
<div class="col-6 col-md">
<h5>Resources</h5>
</div>
<div class="col-6 col-md">
<h5>About</h5>
</div>
</div>
</footer>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
-->
<script>
if (document.readyState == 'loading') {
document.addEventListener('DOMContentLoaded', ready)
} else {
ready()
}
function ready() {
var removeCart = document.getElementsByClassName('btn-danger');
for (j = 0; j < removeCart.length; j++) {
var button = removeCart[j];
button.addEventListener('click', removeele);
}
var changed = document.getElementsByClassName('cart-quantity-input');
for (let j = 0; j < changed.length; j++) {
var input = changed[j];
input.addEventListener('change', qtychanged);
}
var addtoCart = document.getElementsByClassName('addtocart');
for (let i = 0; i < addtoCart.length; i++) {
var addele = addtoCart[i];
addele.addEventListener('click', addtoCartClicked);
}
}
function removeele(event) {
var deletebutton = event.target;
deletebutton.parentElement.parentElement.remove();
updateTotal();
}
function qtychanged(event) {
var input = event.target;
input.addEventListener('change', updateTotal);
updateTotal();
}
function addtoCartClicked(event) {
var button = event.target;
var cartitem = button.parentElement.parentElement;
var itemname = cartitem.getElementsByClassName('display-4')[0].innerText;
var itemSrc = cartitem.getElementsByClassName('product')[0].src;
var itemprice = cartitem.getElementsByClassName('price')[0].innerText;
addToCart(itemSrc, itemname, itemprice);
updateTotal();
}
function addToCart(itemSrc, itemname, itemprice) {
var cartRow = document.createElement('div')
cartRow.classList.add('cartrow')
var cartItems = document.getElementsByClassName('cartitems')[0]
var cartItemNames = cartItems.getElementsByClassName('cart-item-title')
for (var i = 0; i < cartItemNames.length; i++) {
if (cartItemNames[i].innerText == itemname) {
alert('This item has already been added to your cart')
return
}
}
var cartRowContents = `
<span class="cart-item cart-column col">
<img class="cart-item-image" src="${itemSrc}" width="100" height="100">
</span>
<span class='col cart-item-title'>${itemname}</span>
<span class="itemprice cart-column col">${itemprice}</span>
<span class="cart-quantity cart-column col">
<input class="cart-quantity-input" type="number" min='1' value="1" style="width: 50px;">
<button class="btn btn-danger" type="button">Remove</button>
</span>
<br>
`
cartRow.innerHTML = cartRowContents
cartItems.append(cartRow)
cartRow.getElementsByClassName('btn-danger')[0].addEventListener('click', removeele)
cartRow.getElementsByClassName('cart-quantity-input')[0].addEventListener('change', qtychanged)
}
function updateTotal() {
var item = document.getElementsByClassName('cartitems')[0];
var cartrow = item.getElementsByClassName('cartrow');
var total = 0;
for (let i = 0; i < cartrow.length; i++) {
var price = cartrow[i].getElementsByClassName('itemprice')[0];
var quantityele = cartrow[i].getElementsByClassName('cart-quantity-input')[0];
var quantity = quantityele.value;
console.log(quantity);
var acprice = parseFloat(price.innerHTML.replace('$', ''));
total += acprice * quantity;
}
total = Math.round(total * 100) / 100;
document.getElementById('total').innerHTML = "Total: $" + total;
}
function purchaseClicked() {
alert('Thank you for your purchase')
var cartItems = document.getElementsByClassName('cartitems')[0]
while (cartItems.hasChildNodes()) {
cartItems.removeChild(cartItems.firstChild)
}
updateTotal()
}
</script>
</body>
</html>