-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
163 lines (155 loc) · 5.33 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="fontawesome-free-5.9.0-web/css/all.css" rel="stylesheet">
<link href="bootstrap-4/css/bootstrap.css" rel="stylesheet">
<title>starRating App</title>
</head>
<style>
.stars-outer {
position: relative;
display: inline-block;
}
.stars-inner {
position: absolute;
top: 0;
left: 0;
white-space: nowrap;
overflow: hidden ;
width; 0
}
.stars-outer::before {
content: "\f005 \f005 \f005 \f005 \f005";
font-family: "Font Awesome 5 Free";
font-weight: 900;
color: #ccc;
}
.stars-inner::before {
content: "\f005 \f005 \f005 \f005 \f005";
font-family: "Font Awesome 5 Free";
font-weight: 900;
color: #f8ce0b;
}
</style>
<body>
<div class="container mt-5">
<div class="form-group">
<select id="product-select" class ="form-control custom-select " >
<option value="0" disabled selected>Select Product</option>
<option value="sony">Sony 4k Television</option>
<option value="samsung">Samsung 4k Television</option>
<option value="vizio">Vizio 4k Television</option>
<option value="panasonic">Panasonic 4k Television</option>
<option value="phillips">Phillips 4k Television</option>
</select>
<div class="form-group">
<input type="number" id="rating-control" class="form-control" step="0.1" max="5" placeholder="Rate 1 - 5" disabled>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>4K Television</th>
<th>Rating</th>
</tr>
</thead>
<tbody>
<tr class="sony">
<td>Sony 4K Tv</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
<tr class="samsung">
<td>Samsung 4K Tv</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
<tr class="vizio">
<td>Vizio 4K Tv</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
<tr class="panasonic">
<td>Panasonic 4K Tv</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
<tr class="phillips">
<td>Phillips 4K Tv</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
</tbody>
</table>
</div>
</body>
<script>
//initial ratings
const ratings = {
sony: 4.7,
samsung:3.4,
vizio:2.3,
panasonic:3.5,
phillips:4.1,
}
//Total stars
const starsTotal = 5;
//Run get Ratings function
document.addEventListener('DOMContentLoaded',getRatings())
//Form Elements
const productSelect = document.getElementById('product-select');
const ratingControl = document.getElementById('rating-control');
//Init product
let product;
//Product select change
productSelect.addEventListener('change', (e) => {
product = e.target.value;
//Enable rating control
ratingControl.disabled = false;
ratingControl.value = ratings[product];
})
//Rating control change
ratingControl.addEventListener('blur',(e) =>{
const rating = e.target.value;
//Make sure 5 or under
if(rating > 5){
alert('Please rate 1 - 5');
return
}
//Change rating
ratings[product] = rating
getRatings();
})
//Get ratings
function getRatings(){
for(let rating in ratings){
//Get percentage
const starPercentage = (ratings[rating]/starsTotal) * 100;
//Round to nearest 10
const starPercentageRounded = `${Math.round(starPercentage/10)*10}%`;
//set width of stars-inner to percentage
document.querySelector(`.${rating} .stars-inner`).style.width = starPercentageRounded;
//Add the number rating
document.querySelector(`.${rating} .number-rating`).innerHTML = ratings[rating]
}
}
</script>
</html>