-
Notifications
You must be signed in to change notification settings - Fork 1
/
ordering application.html
105 lines (91 loc) · 2.73 KB
/
ordering application.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Shopping Cart</title>
<style>
*{
size: 100px;
font-size: 100px;
border-radius: 100px;
box-shadow: 100px;
}
body{
border: 100px;
border-color: black;
box-sizing: 100px;
size: 100px;
font-size: 100px;
margin-top: 15%;
margin-right: 20%;
margin-left: 40%;
/* padding: 50px; */
box-shadow: rgb(red, green, blue);
box-sizing: border-box;
background-color: pink;
}
h1{
font: blue;
font-size: medium;
font-family: Arial, Helvetica, sans-serif;
color: brown;
}
#cart-list {
list-style-type: none;
padding: 0;
}
#cart-list li {
margin-bottom: 5px;
}
</style>
</head>
<body>
<h1>Simple Shopping Cart</h1>
<label for="item-name">Item Name:</label>
<input type="text" id="item-name" ><br>
<label for="item-price">Item Price:</label>
<input type="number" id="item-price" min="0" step="0.01"><br>
<button onclick="addItem()">Add to Cart</button>
<ul id="cart-list"></ul>
<div>Total Price: $<span id="total-price">0.00</span></div>
<script>
function addItem() {
var itemName = document.getElementById("item-name").value;
var itemPrice = parseFloat(document.getElementById("item-price").value);
if (!itemName || isNaN(itemPrice) || itemPrice <= 0) {
alert("Please enter a valid item name and price.");
return;
}
var item = {
name: itemName,
price: itemPrice
};
addItemToCart(item);
updateTotalPrice();
}
function addItemToCart(item) {
var cartList = document.getElementById("cart-list");
var li = document.createElement("li");
li.textContent = item.name + " - $" + item.price.toFixed(2);
var removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.onclick = function() {
cartList.removeChild(li);
updateTotalPrice();
};
li.appendChild(removeBtn);
cartList.appendChild(li);
}
function updateTotalPrice() {
var total = 0;
var cartList = document.getElementById("cart-list").getElementsByTagName("li");
for (var i = 0; i < cartList.length; i++) {
var itemPrice = parseFloat(cartList[i].textContent.split(" - $")[1]);
total += itemPrice;
}
document.getElementById("total-price").textContent = total.toFixed(2);
}
</script>
</body>
</html>