Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1098099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:31:45+00:00 2026-05-17T00:31:45+00:00

I am still learning jQuery and have come up against something I just can’t

  • 0

I am still learning jQuery and have come up against something I just can’t get my head around? (I’m not great at math!). I know that there is probably a graph type plug-in out there I could use but I don’t know if I need something so bulky/code-heavy. Any help would be greatly appreciated. This website is the best learning source on the web!

I’m trying to figure out of how I could look through a list of comments that have a ‘rating’ of excellent/good/okay/poor contained in a DIV within the li comment. Then work out the percentage of the excellent/good/okay/poor comments based on however many comments there are eg: If there are 4 comments in total, and there are 3 goods and 1 poor, then it would be 75% good and 25% poor.

Then to display those percentages using the width property for each of the corresponding ‘percentage-bar’ DIV’s.

Sorry if this is not clear, I thought it would be so simple, but I’m stumped. Please see the markup below to get an understanding of what I am trying to do.

Any help would be awesome! 🙂

COMMENT // These would be the li comments with the excellent/good/okay/poor ratings within them. These would be wrapped in a UL with id ‘comments’

<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="okay">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="excellent">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="okay">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="poor">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="poor">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="good">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="good">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="good">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="good">It's okay</div>
</li>
<li>
   <p>blah blah blah...</p>
   <div class="user-image"></div>
   <div class="okay">It's okay</div>
</li>

COMMENT // These would be the results, each percentage bar is contained by a DIV that corresponds to a rating excellent/good/okay/poor. These would be wrapped in a UL with id ‘results’.

<li>
    <div class="excellent">
        <div class="percentage-bar"></div>
    </div>
</li>
<li>
   <div class="good">
        <div class="percentage-bar"></div>
    </div>
</li>
<li>
    <div class="okay">
        <div class="percentage-bar"></div>
    </div>
</li>
<li>
    <div class="poor">
        <div class="percentage-bar"></div>
    </div>
</li>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:31:46+00:00Added an answer on May 17, 2026 at 12:31 am

    Here’s a start:
    alt text

    I tried to keep it as simple as possible, so the bars aren’t that pretty.

    jsFiddle example

        // Add a sum function to the Array
    Array.prototype.sum = function(){
        var sum = 0;
        for(var i = 0; i < this.length; sum += this[i++]);
        return sum;
    }
    
    $(function() {
          // set the "size" of the chart
        var size = 400;
    
          // store the rating names
        var ratings = ["excellent", "good", "okay", "poor"];
    
          // zeroed out array for number of votes for each
        var votes   = [0, 0, 0, 0];
    
          // through the `LI`s and I add each vote to the appropriate 
          //   index of the votes array
        $("ul li").each(function(){
            ++votes[ $.inArray($("div:last", this).attr("class"), ratings) ];
        });
    
          // Get the total votes cast
        var total = votes.sum();
    
        $("#chart").attr("height", size);
    
          // Go though each of the votes array elements
          //   and create a bar for each type of vote cast
        $(votes).each(function(index, value) {
              // Calculate length of this one bar.
              // could use something like $("#chart").height() instead of size
            var howTall = (value / total) * size;
            var bar = $("<div>");        
            bar.attr("class", "bar");
            bar.height(howTall);
            bar.width(20); 
            bar.attr("bottom", size);
            var result = $("<div>");
              // Add label of type vote displayed
            result.append(ratings[index]);
            result.attr("class", "result");
              // Add the bar
            result.append(bar);
              // Add the percent shown
            result.append((100*value/total) + "%");
            $("#chart").append(result);        
        })
    });
    

    Of course this can be greatly polished. The bottom of the bars could all be at the same level with the tops varying. Animations could be added as each bar is being added. Gradients could be thrown in… etc etc,

    But the heart of the code is to count up all the votes, store them in an array, and then use the array to calculate the percents and heights of the DIVs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still learning about jQuery, but I have not been able to find a
i have some knowledge of css,jQuery,Grails,django,servlets and jsp still i can't see me designing
Hi I am still learning Jquery and have a problem accessing form elements. My
I'm still learning JQuery (and as a result a little JavaScript) but I can't
I am new to jquery and still learning. The code I have right now
I'm still learning my way around jQuery but I think this may be pretty
I'm still learning Grails and seem to have hit a stumbling block. Here are
I am a C# developer. Still learning. I had not learn all the features
Still learning this jQuery stuff.... I like this ajax page loader; it works for
I'm learning jQuery but I still don't fully undestand how it works. Suppose I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.