JQuery return value for all same name class = 2.53
(first element value applied for all Span)
How do I get different values?
(Edit:) HTML code:
<div class='ratingInfo'>
<table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td>
<div class='review-rating'>10</div>
</td>
<td>
<div class='stars1'></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class='ratingInfo'>
<table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td>
<div class='review-rating'>1</div>
</td>
<td>
<div class='stars1'></div>
</td>
</tr>
</tbody>
</table>
</div>
JavaScript
$( document ).ready(function() {
$( ".stars1" ).html("<span class='stars'>"+$('.review-rating').text()+"</span>");
$('span.stars').stars();
});
$.fn.stars = function() {
return $(this).each(function() {
$(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
});
}
JQuery return value for all same name class = 2.53
(first element value applied for all Span)
How do I get different values?
(Edit:) HTML code:
<div class='ratingInfo'>
<table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td>
<div class='review-rating'>10</div>
</td>
<td>
<div class='stars1'></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class='ratingInfo'>
<table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td>
<div class='review-rating'>1</div>
</td>
<td>
<div class='stars1'></div>
</td>
</tr>
</tbody>
</table>
</div>
JavaScript
$( document ).ready(function() {
$( ".stars1" ).html("<span class='stars'>"+$('.review-rating').text()+"</span>");
$('span.stars').stars();
});
$.fn.stars = function() {
return $(this).each(function() {
$(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
});
}
Share
Improve this question
edited Nov 19, 2014 at 21:06
Roko C. Buljan
207k41 gold badges328 silver badges340 bronze badges
asked Nov 19, 2014 at 20:07
rf jmrf jm
331 silver badge5 bronze badges
3
-
P.S. In your
$.fn.stars
functionthis
is already a jQuery object. You can just doreturn this.each(function(){ //... });
. – gen_Eric Commented Nov 19, 2014 at 21:07 - That wasn't a solution to the problem, I was just pointing it out. – gen_Eric Commented Nov 19, 2014 at 21:15
- Working fine with out table why is that.? – rf jm Commented Nov 19, 2014 at 21:16
1 Answer
Reset to default 10Because $('.review-rating').text()
is grabbing the first element every time, it does not know you want the one that is beside the element. You need to code it to look there.
$( ".stars1" ).each( function () {
var star = $(this);
star.html("<span class='stars'>" + star.prev('.review-rating').text() + "</span>");
});
with the new HTML code, the above will not work since the elements are NOT siblings. That is why it is important to have the actual code! You need to look for a mon parent and find the element within it, in this case you have the TR that contains both. So to get the parent, use closest()
$( ".stars1" ).each( function () {
var star = $(this);
var rating = star.closest("tr").find(".review-rating").text();
star.html("<span class='stars'>" + rating + "</span>");
});