-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.html
More file actions
91 lines (79 loc) · 2.1 KB
/
Copy pathtable.html
File metadata and controls
91 lines (79 loc) · 2.1 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
h2 {
text-align: center;
}
.cart {
border: 1px solid #ccc;
padding: 20px;
margin-bottom: 20px;
}
.item {
margin-bottom: 10px;
}
.item label {
font-weight: bold;
}
button {
padding: 8px 20px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#total {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h2>Shopping Cart</h2>
<div class="cart">
<div class="item">
<label for="apple">Apple:</label>
<input type="number" id="apple" value="0" min="0">
<button onclick="updateTotal(this)">Add</button>
</div>
<div class="item">
<label for="banana">Banana:</label>
<input type="number" id="banana" value="0" min="0">
<button onclick="updateTotal(this)">Add</button>
</div>
<div class="item">
<label for="orange">Orange:</label>
<input type="number" id="orange" value="0" min="0">
<button onclick="updateTotal(this)">Add</button>
</div>
</div>
<span>Total items in cart:<p id="total">0</p></span>
</div>
<script>
function updateTotal(element){
const items = document.querySelectorAll(".item"); //returns a node list of the elements
let count = 0;
items.forEach(function(item){
const value = parseInt(item.querySelector("input").value);
count+= value;
})
document.getElementById("total").innerText = count;
}
</script>
</body>
</html>