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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:28:18+00:00 2026-06-10T05:28:18+00:00

All right, here’s the setup: I have a table in a form whose contents

  • 0

All right, here’s the setup: I have a table in a “form” whose contents I’m editing using the ContentEditable HTML attribute (set on the parent element of the table). There are four columns all together– two sets of “item description” columns and two sets of matching “price” columns, along with an area at the bottom where I’d like the for the total price to be displayed.

Problem #1 is that I’m not sure how to calculate the sum of numbers across cells when I’m not working with inputs and fields. Complicating things (at least to me) is that I’m adding rows to the table dynamically with Jquery.

Problem #2 is that the second set of description/price columns are optional– I’d like to be able to use an “Add” button to trigger individual items in those columns to be added to the total.

Edit: I tried some random things and have had some success by storing the values of the cells in an array, then telling Jquery to add the contents of that array and output the sum. The problem now is that it’s not giving me a sum, it’s giving me the array contents without whitespace. For example, if the array contains 1,2,3 it gives me 123 instead of 6.

Edit #2: After cobbling together bits and pieces of a half dozen tutorials, I’ve got something that works for Problem #1. Now on to problem #2!

var sumArray = []
            $('#calc').click(function(){ 
                $('table tbody tr').each(function() {
                    sumArray.push($(this).find('.cost').text().match(/\d+/));                       
                });
                var total = 0;
                for (var i = 0; i < sumArray.length; i++) {
                    total += parseInt(sumArray[i]);
                }
                $('.totalcost').text(total);

            });

Here’s the table code:

<table>
            <thead>
                <tr>
                        <th>Service</th>
                        <th>Cost</th>
                        <th>Complementary (Optional) Service</th>
                        <th>Cost</th>

                </tr>
            </thead>
            <tbody>
                <tr>
                    <td></td>
                <td class="cost"></td>
                    <td></td>
                    <td class="compcost"></td>
                </tr>
            </tbody>
            <tfoot>
                <tr>

                        <th>Total</th>
                        <th class="totalcost"></th>

                </tr>
            </tfoot>
        </table>
        <span id="add">Add</span>
        <span id="remove">Remove</span>
        <span id="reset">Reset</span>
        <span id="nocomp">No Comps</span>
        <span id="calc">Calculate Total</span>
    </div>

And here’s the JS I’m using right now.

//Add fields to table
            var i = $('tbody>tr').size() + 1;

            $('#add').click(function() {
                if ($("tbody tr:first-child td").length > 2) {
                $('<tr><td></td><td class="cost"></td><td></td><td class="compcost"></td></tr>').fadeIn('slow').appendTo('tbody');
                i++;
                }
                else
                {$('<tr><td></td><td class="cost"></td></tr>').fadeIn('slow').appendTo('tbody');
                i++;
                };

            });

            $('#remove').click(function() {
            if(i > 1) {
                $('tbody>tr:last').remove();
                i--;
            }
            });

            $('#reset').click(function() {
            while(i > 2) {
                $('tbody>tr:last').remove();
                i--;
            }
            });

            $('#nocomp').click(function(){
                if ($("tbody tr td").length > 2) {
                $('thead tr th:nth-child(2),thead tr th:nth-child(3),tbody>tr td:nth-child(2),tbody>tr td:nth-child(3)').remove();
                }
            });

        /*$('#calc').click(function(){
            $('table tbody tr').each(function() {
            $(this).find('.totalcost').text(
            parseFloat($(this).find('.cost').text())
        );
            });
        });*/

        $('#calc').click(function(){ 
            $(this).find('table tbody tr td.cost').each(function(idx)
            {
                var total = $(this).text();
                count += total;                          
            });
            alert(parseInt(count));
        });
  • 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-10T05:28:20+00:00Added an answer on June 10, 2026 at 5:28 am

    Take a look at this jsFiddle, it might be what you want:
    http://jsfiddle.net/mPvyA/3/

    I modified your buttons slightly to add/remove an included class then check for that on calculate:

            $('#exccomp').click(function(){
                $('table').find('.compcost').removeClass('included');
            });
    
            $('#inccomp').click(function(){
                $('table').find('.compcost').addClass('included');
            });
    
            $('#calc').click(function(){ 
                var sumArray = [];
                $('table tbody tr').each(function() {
                    var $this = $(this), 
                        includedCompCost = $this.find('.compcost').filter('.included').text().match(/\d+/);
                    sumArray.push($this.find('.cost').text().match(/\d+/)); 
                    if (includedCompCost !== "") { sumArray.push(includedCompCost); }
                });
                var total = 0;
                for (var i = 0; i < sumArray.length; i++) {
                    var parsedInt = parseInt(sumArray[i]); 
                    if (!isNaN(parsedInt) && parsedInt > 0) { 
                       total += parsedInt; 
                    } 
                }
                $('.totalcost').text(total);
            });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All right so I tried using the button set. So fair, I have been
All right. The problem here is pretty abstract. Bear with me. I have a
All right, so here's a challenge for all you SQL pros: I have a
I have created a HTML page for Blackberry. I have done all right but
Right, Good afternoon all (well, it is afternoon here in the UK!) I am
I'm using the OnIdle-event for some simple animations, and it works all right. The
Right now all I am using to calculate the size are the files in
I have been using a python script for a long while and all of
All right, I'm a novice at python, and here's the snippet of code in
HI all. You can see the code right here: http://jsbin.com/egixa5/5/edit . I'm trying to

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.