1
well I’m creating a star rating system but I’m not able to get jquery to apply the correct Width for each span in the example below I have three DIVS and each of the 3 Divs within 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 take the value of each of the data-avarege and makes 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>
<div class="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 the way it returns this way as in the image
instead of returning like this
Jquery calculates only the 1 div and puts the same result in the others instead of recalculating each one according to the value passed in the data-avarege am beginner ask help from the most experienced
Diogo, first of all I think you’ll have to review your HTML dude, there’s
div
insidespan
. Give it a makeover, otherwise it’s hard!– LeAndrade
I had not seen already arumei
– diogo Dsa