Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript (.each) Problem getting AND appending data for each item.

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #153986
    bcintegrity1
    Participant

    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?

    #153987
    TheDoc
    Member

    Before your get() you’ll want to set a rating var.

    var rating = $(this).find('.rating');
    

    And then in your get():

    rating.append($reviewFive);
    
    #154043
    bcintegrity1
    Participant

    Thank 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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.