File tree Expand file tree Collapse file tree 3 files changed +104
-0
lines changed
Expand file tree Collapse file tree 3 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 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 "> ★</ span >
30+ < span class ="star " data-index ="2 "> ★</ span >
31+ < span class ="star " data-index ="3 "> ★</ span >
32+ < span class ="star " data-index ="4 "> ★</ span >
33+ < span class ="star " data-index ="5 "> ★</ 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 >
Original file line number Diff line number Diff line change 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+ } )
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments