I tried looping the ratings and it is displaying all the values to the same dom element. How do i append it to corresponding dom elements.
This is what tried:
HTML:
<div class="rating" data-id="0"></div>
<div class="rating" data-id="2.5"></div>
<div class="rating" data-id="1.5"></div>
<div class="rating" data-id="3"></div>
<div class="rating" data-id="4.678"></div>
JS:
var ratingValue = "", rounded = "";
var ratingCon = $('.rating');
for(var i=0; i<ratingCon.length; i++){
ratingValue = $(ratingCon[i]).data("id"), rounded = (ratingValue | 0);
for (var j = 0; j < 5 ; j++) {
$(".rating").append('<i class="fa '+ ((j < rounded) ? "fa-star" : ((((ratingValue - j) > 0) && ((ratingValue - j) < 1)) ? "fa-star-half-o" : "fa-star-o")) +'" aria-hidden="true"></i>');
}
}
The issue is in your second for
loop your call $(".rating")
though selecting all elements with the className rating
is ultimately just using the first .rating
element when you append. If you change $(".rating")
to $(ratingCon[i])
it will all work. Updated fiddle: https://jsfiddle.net/32L669tv/12/ .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.