-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstars_rating.html
60 lines (51 loc) · 1.26 KB
/
stars_rating.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
<html>
<head>
<style>
#star-wrapper{
display:flex;
flex-direction:row;
}
.star{
color:lightgrey;
font-size:20px;
}
.highlight{
color:#e3e336;
}
</style>
</head>
<body>
<div id="star-wrapper">
<div id="star-1" class="star">★</div><div id="star-2" class="star">★</div><div id="star-3" class="star">★</div><div id="star-4" class="star">★</div><div id="star-5" class="star">★</div>
</div>
<div id="test"></div>
<script>
for (var i=1;i<=5;i++){
var buttonId="star-"+i.toString();
document.getElementById(buttonId).addEventListener("click", setRating);
document.getElementById(buttonId).newScore=i;
}
function setRating(event){
var score = event.currentTarget.newScore;
document.getElementById("test").innerHTML = score;
var previousScore=0;
for (var i=1;i<=5;i++){
var buttonId="star-"+i.toString();
var isStarChecked = document.getElementById(buttonId).classList.contains("highlight");
if (isStarChecked){
previousScore=i;
}
document.getElementById(buttonId).classList.remove("highlight");
}
if (score!=previousScore) {
for (var i=1;i<=score;i++){
var buttonId="star-"+i.toString();
document.getElementById(buttonId).classList.add("highlight");
}
} else {
document.getElementById("test").innerHTML = "";
}
}
</script>
</body>
</html>