forked from Sharma-NareshIT/WebDevelopment-11AM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingcart.html
140 lines (123 loc) · 5.4 KB
/
shoppingcart.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css">
<script>
var categories = ["Select Category", "Electronics", "Shoes"];
var electronics = ["Select Electronic Product","Samsung TV", "Mobile"];
var shoes = ["Select Shoe Product","Nike Casuals", "Lee Cooper Boot"];
var products = [
{Name: "Samsung TV", Price: 35000.56, Photo:"../Images/tv.jpg"},
{Name: "Mobile", Price: 15000.56, Photo:"../Images/mobile.jpg"},
{Name: "Nike Casuals", Price: 5000.56, Photo:"../Images/shoe.jpg"},
{Name: "Lee Cooper Boot", Price: 2000.56, Photo:"../Images/shoe1.jpg"},
]
function bodyload() {
lstCategories = document.getElementById("lstCategories");
for(var i=0; i<categories.length; i++) {
var option = document.createElement("option");
option.text = categories[i];
option.value = categories[i];
lstCategories.appendChild(option);
}
}
function AddProducts(list){
lstProducts = document.getElementById("lstProducts");
for(var i=0; i<list.length; i++) {
var option = document.createElement("option");
option.text=list[i];
option.value=list[i];
lstProducts.appendChild(option);
}
}
function CategoryChanged(){
var category = document.getElementById("lstCategories").value;
switch(category)
{
case "Electronics":
document.getElementById("lstProducts").innerHTML="";
AddProducts(electronics);
break;
case "Shoes":
document.getElementById("lstProducts").innerHTML="";
AddProducts(shoes);
break;
default:
document.getElementById("lstProducts").innerHTML="";
alert("Please Select a Category");
break;
}
}
var product;
function ProductChanged() {
var productName = document.getElementById("lstProducts").value;
product = products.filter(x=>x.Name==productName);
document.getElementById("lblName").innerHTML=product[0].Name;
document.getElementById("lblPrice").innerHTML = "₹" + product[0].Price;
document.getElementById("imgProduct").src= product[0].Photo;
}
var cartItems = [];
function AddToCartClick(){
cartItems.push(product[0]);
for(var i=0; i<cartItems.length; i++) {
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
td1.innerHTML=product[0].Name;
td2.innerHTML=product[0].Price;
var pic = new Image();
pic.src= product[0].Photo;
pic.width="50";
pic.height="50";
td3.appendChild(pic);
var tr = document.createElement("tr");
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
}
document.getElementById("tbody").appendChild(tr);
}
</script>
</head>
<body onload="bodyload()">
<div class="container text-center">
<h2 class="text-primary">Amazon Shopping</h2>
<div class="form-group">
<label>Select a Category</label>
<select id="lstCategories" class="form-control" onchange="CategoryChanged()">
</select>
</div>
<div class="form-group">
<label>Select a Product</label>
<select id="lstProducts" class="form-control" onchange="ProductChanged()">
</select>
</div>
<div class="form-group">
<div class="card">
<div class="card-header">
<h2 id="lblName"></h2>
</div>
<div class="card-body">
<img id="imgProduct" class="img-thumbnail" width="200" height="200">
</div>
<div class="card-footer">
<h3 id="lblPrice"></h3>
<input type="button" value="Add to Cart" class="btn btn-primary" onclick="AddToCartClick()">
</div>
</div>
</div>
<div class="form-group">
<h2>Your Cart Items</h2>
<table class="table table-warning">
<thead>
<th>Name</th>
<th>Price</th>
<th>Preview</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
</body>
</html>