SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    table cell math question

    hi!
    i wonder if there is a simply script for what i need....
    example i have a table which have some number values and when i double click on the first value in choosen column the script make a sum of all values into that column!
    any suggestion appreciated!
    thanx in advance!!!

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I think I am getting a little idea of what you want to accomplish, but it would be alot easier if you could post an example of how the table can look, and what the result would be.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok
    this is the table...
    Code:
    +-----+-----+
    |   1 |   2 |
    +-----+-----+
    | 234 |  36 |
    +-----+-----+
    |  2  |   7 |
    +-----+-----+
    now when i click on 1 i get the result of 234 + 2 and if i click on 2 i get the result of 36 + 7....
    hope im clear!

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The following code have been tested in Mozilla Firefox 1.0 and IE6.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <title>Javascript: Count sum of column values</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <script type="text/javascript">
       function count(tableId, column)
       {
         var tableRows = document.getElementById(tableId).rows;
         for( var i = 1, result = 0; i < tableRows.length; i++ )
           result += new Number(tableRows[i].cells[column].innerHTML);
         
         alert(result);
       }
      </script>
     </head>
     
     <body>
      <table border="1" cellpadding="5" id="count_this">
       <tr>
        <td ondblclick="count('count_this', 0)">1</td>
        <td ondblclick="count('count_this', 1)">2</td>
       </tr>
       <tr>
        <td>234</td>
        <td>36</td>
       </tr>
       <tr>
        <td>2</td>
        <td>7</td>
       </tr>
      </table>
     </body>
    </html>
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx lilleman...
    another little question...
    what if i use - or / or * or %
    thanx...

  6. #6
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Use -=, /=, *= or %= instead of +=.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...
    and if i have to add or subtract 20% of the total?

  8. #8
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try adding the following line after the loop (outside of it, that is).

    Code:
    result += ( result * 0.2 );
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much lilleman!!!
    u helped me a lot!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •