Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Created March 4, 2018 18:38
Show Gist options
  • Save JackHowa/90994ccbbaf90063d4b69a4ae425659c to your computer and use it in GitHub Desktop.
Save JackHowa/90994ccbbaf90063d4b69a4ae425659c to your computer and use it in GitHub Desktop.
star it
<div id='rating'>
<span>*</span>
<span>*</span>
<span>*</span>
<span>*</span>
<span>*</span>
</div>
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 = "";
}
});
}
.active {
color:red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment