A Pen by Jack Howard on CodePen.
Created
March 4, 2018 18:38
-
-
Save JackHowa/90994ccbbaf90063d4b69a4ae425659c to your computer and use it in GitHub Desktop.
star it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id='rating'> | |
<span>*</span> | |
<span>*</span> | |
<span>*</span> | |
<span>*</span> | |
<span>*</span> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stars = document.querySelectorAll('#rating span'); | |
stars.forEach(star => { | |
star.addEventListener('click', handleClick); | |
}); | |
function handleClick(e) { | |
let above = true; | |
// loop through all of these | |
stars.forEach(star => { | |
// check if this equal to itself | |
if (star === this) { | |
above = false; | |
} | |
if (above || star === this) { | |
star.className = "active"; | |
} else { | |
star.className = ""; | |
} | |
}); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.active { | |
color:red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment