Reputation: 384
I'm trying to loop through all of the .starwrapper divs on a page, then find some text inside of that div and then add it to be displayed at the end of the div.
Like so:
<script type="text/javascript">
jQuery( document ).ready(function() {
$('.starwrapper').each(function( index ) {
var starvalue = $('.starwrapper .rating').text();
$(this).children('.star-rating').append(starvalue);
});
});
</script>
Trouble is, every one of them gets the first starvalue variable's value. Might anyone know what I'm doing wrong with that .each()?
Upvotes: 0
Views: 62
Reputation: 869
I'm not sure but try this for starvalue variable:
var starvalue = $('.rating', $(this)).text();
Upvotes: -1
Reputation: 3010
You have to find the text from the current element $(this)
and not globally, so try to replace
var starvalue = $('.starwrapper .rating').text();
with
var starvalue = $(this).find('.rating').text();
Upvotes: 1
Reputation: 2587
Your problem is that this line:
var starvalue = $('.starwrapper .rating').text();
Is effectively absolute, it searches for all .starwrapper
elements, not taking $this
into account.
Try this instead:
<script type="text/javascript">
jQuery( document ).ready(function() {
$('.starwrapper').each(function( index ) {
var starvalue = $(this).find('.rating').text();
$(this).children('.star-rating').append(starvalue);
});
});
</script>
Upvotes: 2