Forums

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

Home Forums JavaScript Jquery to append each closest element

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

    I am using .get to retrieve data to obtain a “star rating average” for members listed on my site. The averages will then be appended to .rating within each member’s listing.

    I have no problem “getting” each member’s review file (thanks to @Mottie and @vindicateme), and counting the number of five star reviews. The problem is, EACH total is appended to EVERY .rating.

    My test page shows this well. Kim has one review, David has two reviews. But “21” is appended to each of their listings :^( I will work out the details to obtain each average after I get this working.

    HTML

    <li class="services">
    <div class="lpic">
    <div class="lbox hbox">
    <div class="pro"><img src="images/DavidHarper-list.jpg" />
    <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="images/KimMedina.jpg" />
    <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");
         var $review5 = $reviewFive.length;
    
         $('.rating').append($review5);
        });
    });
    

    Thanks for any help!

    #153879
    bcintegrity1
    Participant

    I shortened the code for simplicity, but still the same issue.

    $('.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);
        });
    });
    
    #154071
    bcintegrity1
    Participant

    Hello @Mottie

    Thank you for the response. I re-posted this thread here (since I couldn’t edit it) to clarify the issue for better understanding.

    This issue has since been solved. Again, THANK YOU:)

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