Skip to content

Commit 017967e

Browse files
committed
Initial commit
0 parents  commit 017967e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<!-- Please NOTE that the link to assets (like images) need to be set in global context.
4+
For example:
5+
To access an image from assets folder correct way to set src attribute is
6+
7+
src = 'profile.png'
8+
9+
instead of
10+
11+
src = './assets/profile.png'
12+
-->
13+
14+
<head>
15+
<meta charset="UTF-8" />
16+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
17+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
18+
<title>Ticket Management App</title>
19+
20+
<link rel="stylesheet" href="style.css">
21+
22+
23+
</head>
24+
25+
<body>
26+
<div class="container">
27+
<h1>Star Rating Component</h1>
28+
<div id="star-container" class="star-container">
29+
<span class="star" data-index="1">&#9733;</span>
30+
<span class="star" data-index="2">&#9733;</span>
31+
<span class="star" data-index="3">&#9733;</span>
32+
<span class="star" data-index="4">&#9733;</span>
33+
<span class="star" data-index="5">&#9733;</span>
34+
</div>
35+
<h2>Rating Count:
36+
<span id="count">0</span>
37+
</h2>
38+
</div>
39+
<script src="script.js"></script>
40+
</body>
41+
42+
</html>

script.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const starCont = document.querySelector('.star-container');
2+
const allStars = document.querySelectorAll('.star');
3+
const count = document.getElementById('count');
4+
let idx = 0;
5+
let clickFlag = false;
6+
starCont.addEventListener('mouseover', function(e){
7+
allStars.forEach(function(str){
8+
str.classList.remove('star-filled');
9+
});
10+
idx = 0;
11+
if(e.target.classList[0] === 'star'){
12+
idx = e.target.getAttribute('data-index');
13+
allStars.forEach(function(str){
14+
if(str.getAttribute('data-index') <= idx){
15+
str.classList.add('star-filled');
16+
}
17+
});
18+
}
19+
});
20+
21+
starCont.addEventListener('mouseout', function(e){
22+
if(!clickFlag){
23+
allStars.forEach(function(str){
24+
str.classList.remove('star-filled');
25+
});
26+
idx = 0;
27+
}
28+
});
29+
30+
starCont.addEventListener('click', function(e){
31+
clickFlag = true;
32+
if(e.target.classList[0] === 'star'){
33+
idx = e.target.getAttribute('data-index');
34+
allStars.forEach(function(str){
35+
if(str.getAttribute('data-index') <= idx){
36+
str.classList.add('star-filled');
37+
}
38+
});
39+
count.innerText = idx;
40+
}
41+
})

style.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.container {
2+
display: flex;
3+
flex-direction: column;
4+
align-items: center;
5+
}
6+
7+
.star-container {
8+
font-size: 40px;
9+
/* background-color: grey; */
10+
}
11+
12+
.star {
13+
padding: 5px;
14+
color: gray;
15+
cursor: pointer;
16+
/* background-color: blue; */
17+
}
18+
19+
.star-filled {
20+
color: #ffdf00;
21+
}

0 commit comments

Comments
 (0)