I'm trying to figure out how to write a javascript function that takes an element + a score and uses jQuery addClass
and removeClass
to display the correct star rating (rounded to the nearest half star) but having problems... When I was using a "whole star" system I used something this system:
function() {
$(this).prevAll().andSelf().addClass('active');
$(this).nextAll().removeClass('active');
}
Which no longer works for a half-star system. Does anyone have any advice? Here is the code I'm using now:
function populateStars(stars, score) {
// Stars refers to the <ul> container with <li> stars inside (see later code).
// This function should use stars (container) and score (rating between 1-5) to display stars
}
This is an example of the elements that the "stars" var refers to:
<ul class="big-stars">
<li class="star-1 full">1</li>
<li class="star-2 full">2</li>
<li class="star-3 half">3</li>
<li class="star-4">4</li>
<li class="star-5">5</li>
</ul>
And the CSS that controls whether the star is full, empty or half-full.
.big-stars li {
text-indent: -9999px;
width: 49px;
height: 46px;
background: url('../images/big_star_empty.png');
}
.big-stars .full {
background: url('../images/big_star_full.png');
}
.big-stars .half {
background: url('../images/big_star_half.png');
}