Home › Forums › JavaScript › (.each) Problem getting AND appending data for each item.
- This topic is empty.
-
AuthorPosts
-
October 24, 2013 at 9:18 pm #153986bcintegrity1Participant
I couldn’t edit my other thread, so I’m re-posting this, hopefully with more clarity:)
The first part of my code gathers info (first and last name) within each
.lbox
(a member’s listing) to construct a url to retrieve that member’s review page. Then I’m using the.get
function to retrieve data from the “constructed” url, do something to it, and append it to the.rating
div in the same.lbox
.The problem is, the data is appended to EVERY
.rating
. In short, it seems the.each
applies to each.lbox
, but.get
applies all data to every ‘.lbox’. You can see the problem here on my test page.My HTML
<li class="services"> <div class="lpic"> <div class="lbox hbox"> <div class="pro"><img src="" /> <p>David Harper<br /> (804) 938-6500<br /> Dodson Pest Control</p> <div class="proviewtab"><p3>Reviews</p3></div></div> <div class="rating">AVERAGE RATING</div> <div class="ratingimage"></div></div></div></li> <li class="services"> <div class="lpic"> <div class="lbox hbox"> <div class="pro"><img src="" /> <p>Kim Medina<br /> (804) 381-8026<br /> Dodson Pest Control</p><div class="proviewtab"><p3>Reviews</p3></div></div> <div class="rating">AVERAGE RATING</div> <div class="ratingimage"></div></div></div></li>
My Code
$('.lbox').each(function(event) { var name = $(this).find("p").text(); name = name.split(/^(\w+)\s+(\w+)/); var firstName = name[1]; var lastName = name[2]; var folder = "reviews-pest-control-va"; var reviewsPath = folder + '/' + lastName + ',' + firstName + '.php'; reviewsFile = reviewsPath.toLowerCase(); $.get(reviewsFile, function (data) { var $reviewFive = $(data).find(".five").length; $('.rating').append($reviewFive); }); });
What am I missing?
October 24, 2013 at 10:33 pm #153987TheDocMemberBefore your
get()
you’ll want to set arating
var.var rating = $(this).find('.rating');
And then in your
get()
:rating.append($reviewFive);
October 25, 2013 at 11:57 am #154043bcintegrity1ParticipantThank you @TheDoc!!! Hot dang:) You can see it working on the link to my test page above. For now, I will use it to display the total number of reviews…but more to come.
Again, thank you.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.