The Situation:
I am currently working on a system where users can rate various products using Laravel. Each product has an average rating which is then used to display stars. These ratings are shown on the "Product Listing" page, which contains multiple products.
My Objective:
I am looking to pass a variable through a div in order to populate the stars when the page loads. I have attempted the following code:
<span class="rating" rating="3">
<span class="star "></span>
<span class="star "></span>
<span class="star"></span>
<span class="star"></span>
<span class="star"></span>
</span>
Here is the jQuery script I have tried:
$('.rating').each(function (event) {
var rating = $(this).attr('rating');
for(var i = 0; (i < rating); i++)
{
$('.rating span.star').eq(i).addClass('filled');
}
});
However, I am facing issues with the stars being populated incorrectly and only taking into account the last product's rating while ignoring the rest.
You can see a live example here: