[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

javascript for calculating total for a column

Posted on 2009-02-08
4
Medium Priority
?
624 Views
Last Modified: 2012-05-06
In my HTML document, I have a table contains following fields:

StudentID|ExamScore1|ExamScore2
00001|90|100
00002|70|100
00003|60|80

How do I write a javascript to calculate the total for ExamScore1 and ExamScore2 columns? So my table will look like this:
00001|90|100
00002|70|100
00003|60|80
Total|220|280

Thanks for your help.



0
Comment
Question by:dennis231
  • 2
4 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23586181
You mean HTML table? If so then is the number of rows going to remain the same?
0
 

Author Comment

by:dennis231
ID: 23586410
Yes, it is a html table, but the number of rows will be changed from time to time.

Sample table:
<table border="1" width="822">
    <tr>
        <td width="266">StudentID</td>
        <td width="266">
            <p><span class="value">ExamScore1</span></p>
        </td>
        <td width="266">
            <p><span class="value">ExamScore2</span></p>
        </td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00001</span></p>
        </td>
        <td width="266">90</td>
        <td width="266">100</td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00002</span></p>
        </td>
        <td width="266">70</td>
        <td width="266">100</td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00003</span></p>
        </td>
        <td width="266">60</td>
        <td width="266">80</td>
    </tr>
    <tr>
        <td width="266" bgcolor="aqua">Total</td>
        <td width="266" bgcolor="aqua"><b>220 &nbsp;<-calculate total</b></td>
        <td width="266" bgcolor="aqua">
            <p><b>280 <-calculate total</b></p>
        </td>
    </tr>
</table>
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23586441
I can do it for you tomorrow if you can wait. Time for me to sleep now..
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 23586825

<?xml version="1.0" encoding="UTF-8"?>
<!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></title>
<script type="text/javascript">
function init()
{
	calculateTotal()
}
window.onload=init;
function calculateTotal(){
	var t=document.getElementById("myTable");
	var totals=[0,0,0];
 
	for( var row=1, limit=t.rows.length-1; row < limit; ++row)
	{
		for( var col=1,cellLimit=t.rows[row].cells.length; col < cellLimit; ++col)
		{
			totals[col] += parseInt(t.rows[row].cells[col].innerHTML);
		} 
	}
	t.rows[t.rows.length-1].cells[1].innerHTML=totals[1];
	t.rows[t.rows.length-1].cells[2].innerHTML=totals[2];
}
</script> 
</head>
<body>
<table id="myTable" border="1" width="822">
    <tr>
        <td width="266">StudentID</td>
        <td width="266">
            <p><span class="value">ExamScore1</span></p>
        </td>
        <td width="266">
            <p><span class="value">ExamScore2</span></p>
        </td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00001</span></p>
        </td>
        <td width="266">90</td>
        <td width="266">100</td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00002</span></p>
        </td>
        <td width="266">70</td>
        <td width="266">100</td>
    </tr>
    <tr>
        <td width="266">
            <p><span class="value">00003</span></p>
        </td>
        <td width="266">60</td>
        <td width="266">80</td>
    </tr>
    <tr>
        <td width="266" bgcolor="aqua">Total</td>
        <td width="266" bgcolor="aqua"><b>220 &nbsp;<-calculate total</b></td>
        <td width="266" bgcolor="aqua">
            <p><b>280 <-calculate total</b></p>
        </td>
    </tr>
</table>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question