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 8668865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:19:09+00:00 2026-06-12T18:19:09+00:00

I’m trying to use JS to sum up a column of already Javascript-generated values.

  • 0

I’m trying to use JS to sum up a column of already Javascript-generated values. I’m new to JS, so this may be way wrong. At any rate, I tried this:

NOTE — FINAL CODE AT BOTTOM

 $(".js-package").change(function(){
        var parentTable = $(this).parents("table");
        var table_rows = parentTable.rows;
        var height = table_rows.length;
        var total = 0;
        for (var i = 0; i < height; i++) {
            var current_row = table_rows[i];
            total += current_row[5];
        }
        $(parentTable).find(".js-lb-total").html((total).toFixed(2));
 });

This applies to a bunch of html, but the relevant stuff is that I’ve got this line where things are supposed to total up:

<td class="js-lb-total">?</td>

And this further up:

 <td>
   <%= l.select :units, dropdown, {}, :class => "unit_select js-package" %>
 </td>

Importantly, the seemingly arbitrary number 5 refers to the column (assuming JS starts arrays at 0) that I’m trying to sum up.

Any idea what I’m doing wrong/how to fix it? I’ll go ahead and look into opening a fiddle that I can link to with the more complete code. I’ll add that link below.

Thanks!

EDIT — Row totaling script below

 $(".js-package").change(function(){
        var numOfPackages = parseFloat($(this).val());
        var parentTr = $(this).parents("tr");
        var parentTable = $(this).parents("table");
        var weight = parseFloat($(parentTr).find(".js-weight").attr('data-weight'));
        var price = parseFloat($(parentTr).find(".js-lb-price").attr('data-lb-price'));
        $(parentTr).find(".js-price").html(((numOfPackages * weight * price).toFixed(2)));
        $(parentTr).find(".js-lbs").html((numOfPackages * weight).toFixed(2));
    });

EDIT 2 — Basic fiddle link here Fiddle. None of the JS is working there, though, for some reason. (The first bunch works on my server). So it may not be particularly helpful.

EDIT 3 — To be clear, I’m trying to sum a column whose values are all dynamically generated by another javascript action. They’re not in the html. Could that be part of the problem?

FINAL EDIT — After much tweaking and following of advice, I got this, which works great (and totals both price and poundage, after totaling each line).

$(".js-package").change(function(){
        var numOfPackages = parseFloat($(this).val());
        var parentTr = $(this).parents("tr");
        var parentTable = $(this).parents("table");
        var weight = parseFloat($(parentTr).find(".js-weight").attr('data-weight'));
        var price = parseFloat($(parentTr).find(".js-lb-price").attr('data-lb-price'));
        $(parentTr).find(".js-price").html(((numOfPackages * weight * price).toFixed(2)));
        $(parentTr).find(".js-lbs").html((numOfPackages * weight).toFixed(2));

    var table = document.getElementById('sumtable');
    var table_rows = table.rows;
    var height = parseInt(table_rows.length);
    var lb_total = 0;      
    var money_total = 0;
    var cell;   
    for (var i = 1, iLen = height - 1; i < iLen; i++) {
        cell = table_rows[i].cells[5];
        lb_total += Number(cell.textContent);
    }
    for (var j = 1, jLen = height - 1; j < jLen; j++) {
        cell = table_rows[j].cells[6];
        money_total += Number(cell.textContent);
    }

    $(parentTable).find(".js-lb-total").html(lb_total.toFixed(2));
    $(parentTable).find(".js-price-total").html(money_total.toFixed(2));
});
  • 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-06-12T18:19:11+00:00Added an answer on June 12, 2026 at 6:19 pm

    The example below might get you started. If the headers and footers are in a different table section, it will make life easier, I’ve put them all in the one tbody.

    Things to note:

    1. Values read from cells will be strings, so you need to convert them to numbers
    2. Javascript is notoriously bad at decimal arithmetic, much better to do integer arithmetic and convert to decimal only at the very end for presentation
    3. Be careful of Math.toFixed, it has quirks, search the questions

    Good luck. 🙂

    <script>
    // column is the column with values in it to total (first column is zero)
    // Assume values are floats.
    function addRows(tableId, column, resultId) {
    
        var table = document.getElementById(tableId);
        var rows = table.rows;
        var total = 0;
        var cell;
    
        // Assume first row is headers, adjust as required
        // Assume last row is footer, addjust as required
        for (var i=1, iLen=rows.length - 1; i<iLen; i++) {
            cell = rows[i].cells[column];
            total += Number(cell.textContent || cell.innerText);
        }
        document.getElementById(resultId).innerHTML = total.toFixed(2);
    }
    </script>
    
    <table id="productTable">
      <tr>
        <th>Item
        <th>value ($)
      <tr>
        <td>foo
        <td>23.33
      <tr>
        <td>bar
        <td>03.04
      <tr>
        <td>Total
        <td id="totalValue">
    </table>
    <button onclick="addRows('productTable', 1, 'totalValue')">Update total</button>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. 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.