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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:28:48+00:00 2026-05-20T10:28:48+00:00

I’m a bit stuck trying to loop through a table row, calculate the values

  • 0

I’m a bit stuck trying to loop through a table row, calculate the values (which are then output) then move onto the next row. Seems to work fine for the first row?

Here’s a code snippet and jsfiddle link:

var rows = $("#invoice_table tr:gt(0)"); // skip the header row

        rows.each(function(index) {
            rows.children("td").each(function() { // calculate amount, vat, subtotal for row
                qty = $("td:nth-child(3) input").val();
                rate = $("td:nth-child(4) input").val();
                amount = qty * rate;
                vat = amount / 100 * 20;
                subtotal = amount;
                subtotal += vat;

                vat = vat.toFixed(2); // limit to two decimal places
                amount = amount.toFixed(2);
                subtotal = subtotal.toFixed(2);

                $("td:nth-child(5) input").val(amount); // output the values
                $("td:nth-child(6) input").val(vat);
                $("td:nth-child(7) input").val(subtotal);
            });
        });

http://jsfiddle.net/8QK2E/48/

Thanks!

EDIT:

Okay so being the stubborn man I am, I forged ahead with my own ideas. This is the code I ended up with. Totals the rows, then adds up the columns.

$("#invoice_table").live({ // invoice calculations
    blur: function() {
    var qty = '0.0';
    var rate = '0.0';
    var amount = '0.0';
    var amttotal = '0.0';
    var vat = '0.0';
    var vattotal = '0.0';
    var subtotal = '0.0';
    var total = '0.0';
    var num_tr = $('#invoice_table tr').length;
    num_tr = num_tr-2; // skip totals row

    amount = amount * 1; // force js to handle the var as a number
    qty = qty * 1;
    rate = rate * 1;
    vat = vat * 1;
    amttotal = amttotal * 1;            
    vattotal = vattotal * 1;   
    total = total * 1;

    for(var i = 1;i < num_tr;i++) {
    var row = $('#invoice_table tr:nth-child('+i+')'); // skip the header row

    row.children("td").each(function() { // calculate amount, vat, subtotal for row
    qty = $('tr:nth-child('+i+') td:nth-child(3) input').val();
    rate = $('tr:nth-child('+i+') td:nth-child(4) input').val();
    amount = qty * rate;
    vat = amount / 100 * 20;
    subtotal = amount;
    subtotal += vat;

    vat = vat.toFixed(2); // limit to two decimal places
    amount = amount.toFixed(2);
    subtotal = subtotal.toFixed(2);

    $('tr:nth-child('+i+') td:nth-child(5) input').val(amount); // output the values
    $('tr:nth-child('+i+') td:nth-child(6) input').val(vat);
    $('tr:nth-child('+i+') td:nth-child(7) input').val(subtotal);
    });

    }

    for(var i = 2;i < num_tr;i++) {
    var rows = $('#invoice_table tr:nth-child('+i+')'); // skip the header row

    amttotal += parseFloat($('tr:nth-child('+i+') td:nth-child(5) input').val());
    vattotal += parseFloat($('tr:nth-child('+i+') td:nth-child(6) input').val());
    total    += parseFloat($('tr:nth-child('+i+') td:nth-child(7) input').val());
    }

    amttotal = amttotal.toFixed(2); // limit to two decimal places
        vattotal = vattotal.toFixed(2);
        total = total.toFixed(2);

        $("#total_amount input").val(amttotal);
        $("#total_vat input").val(vattotal);
        $("#grand_total input").val(total);
    }
});
  • 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-20T10:28:49+00:00Added an answer on May 20, 2026 at 10:28 am

    First up I can see a problem, in your first rows.each you are then looping through all td that are children of rows. Instead use this to limit it to the current row that you are looping through. Also, you don’t have any scope for your qty and rate parts, you are looking at all tds as well.

    You may need to adjust the selectors as stuff for the outputs as I don’t actually know your layout or where you are trying to display the outputs.

    var rows = $("#invoice_table tr:gt(0)"); // skip the header row
    
        rows.each(function(index) {
            var qty_input = $("td:nth-child(3) input", this);
            var rate_input = $("td:nth-child(4) input", this);
            $(this).children("td").each(function() { // calculate amount, vat, subtotal for row
                var qty = qty_input .val();
                var rate = rate_input .val();
                var amount = qty * rate;
                var vat = amount / 100 * 20;
                var subtotal = amount;
                subtotal += vat;
    
                vat = vat.toFixed(2); // limit to two decimal places
                amount = amount.toFixed(2);
                subtotal = subtotal.toFixed(2);
    
                $('#total_amount').val(amount); // output the values
                $('#total_vat').val(vat);
                $('#grand_total').val(subtotal);
            });
        });
    

    Potentially though, you could bind to the change events for each input field and only do the calculations then. If you can edit your source to add some more classes you could do something like:

    var inputs = $('#invoice_table tr:gt(0) td.quantity input, #invoice_table tr:gt(0) td.rate input');
    var amount_inputs = $('#invoice_table tr:gt(0) td.amount input');
    var vat_inputs = $('#invoice_table tr:gt(0) td.vat input');
    var subtotal_inputs = $('#invoice_table tr:gt(0) td.subtotal input');
    
    inputs.change(function() {
        // Finding them this way purely so that we don't duplicate the code,
        // it would be faster to separate the inputs array into their type and bind the change appropriately.
        var qty = $(this).closest('tr').find('td.quantity input').val();
        var rate = $(this).closest('tr').find('td.rate input').val();
        var amount_input = $(this).closest('tr').find('td.amount input');
        var vat_input = $(this).closest('tr').find('td.vat input');
        var subtotal_input = $(this).closest('tr').find('td.subtotal input');
    
        var amount = qty * rate;
        var vat = amount / 100 * 20;
        var subtotal = amount + vat;
    
        amount_input.val(amount.toFixed(2));
        vat_input.val(vat.toFixed(2));
        subtotal_input.val(subtotal.toFixed(2));
    
        update_totals();
    });
    
    function update_totals() {
        var amttoal = 0;
        var vattotal = 0;
        var total = 0;
    
        amount_inputs.each(function(){
            amttoal += this.val();
        });
        vat_inputs.each(function(){
            vattotal += this.val();
        });
        subtotal_inputs.each(function(){
            vattotal += this.val();
        });
    
        $("#total_amount input").val(amttoal.toFixed(2));
        $("#total_vat input").val(vattotal).toFixed(2);
        $("#grand_total input").val(vattotal.toFixed(2));
    }
    

    Not sure if it’d be faster or easier though.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.