I'm creating a star rating system but I can not get the jquery to apply the correct Width for each span in the example below. I have three DIVs and each of the 3 divs inside them has
<span class="ratingAverage" data-average="1.2"></span>
<span class="ratingAverage" data-average="4.2"></span>
<span class="ratingAverage" data-average="5.6"></span>
Then with jquery I get the value of each of the data-avarege and does the calculation and increases the bar in%
< script type = "text/javascript" >
$(function() {
var average = $('.ratingAverage').attr('data-average');
function avaliacao(average) {
average = (Number(average) * 20);
$('.bg').css('width', 0);
$('.barra .bg').animate({
width: average + '%'
}, 500);
}
avaliacao(average);
}); <
/script>
<!-- begin snippet: js hide: false console: true babel: false -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="col col-12">
<div class="about">
<span class="ratingAverage" data-average="1.2"></span>
<span class="article" data-id=""></span>
<div class="barra">
<span class="bg"></span>
<div class="overhiden">
<span class="stars">
<span class="star" data-vote="1"></span>
<span class="star" data-vote="2"></span>
<span class="star" data-vote="3"></span>
<span class="star" data-vote="4"></span>
<span class="star" data-vote="5"></span>
</span>
</div>
</div>
</div>
</div>
<div class="col col-12">
<div class="about">
<span class="ratingAverage" data-average="4.2"></span>
<span class="article" data-id=""></span>
<div class="barra">
<span class="bg"></span>
<div class="overhiden">
<span class="stars">
<span class="star" data-vote="1"></span>
<span class="star" data-vote="2"></span>
<span class="star" data-vote="3"></span>
<span class="star" data-vote="4"></span>
<span class="star" data-vote="5"></span>
</span>
</div>
</div>
</div>
</div>
<div class="col col-12">
<div class="about">
<span class="ratingAverage" data-average="5.6"></span>
<span class="article" data-id=""></span>
<div class="barra">
<span class="bg"></span>
<div class="overhiden">
<span class="stars">
<span class="star" data-vote="1"></span>
<span class="star" data-vote="2"></span>
<span class="star" data-vote="3"></span>
<span class="star" data-vote="4"></span>
<span class="star" data-vote="5"></span>
</span>
</div>
</div>
</div>
</div>
The code of the way you return it this way as in the image
insteadofreturninglikethis
JQuery makes the calculation of 1 div and puts the same result in the others instead of redoing each one according to the value passed on the date-avarege I am beginner I ask for help from the most experienced