473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

adding column totals

Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
ttot += document.forms[0].total(i).value;
}
document.forms[0].subtot.value = format(ttot);
}
}

I know the "isPosInt" function works because it works for other things and
Mike wrote it...so I did not add it.
And I do have a global varible elsewhere var e = document.forms[0].elements,
but I also can't get "if(isPosInt(ea['total' + i].value))" to work
Jul 23 '05 #1
4 1427
"Abby Lee" <ab*******@hotmail.com> wrote in message
news:ui***************************@news.ks.uiuc.ed u...
Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
ttot += document.forms[0].total(i).value;
}
document.forms[0].subtot.value = format(ttot);
}
}

I know the "isPosInt" function works because it works for other things and
Mike wrote it...so I did not add it.
And I do have a global varible elsewhere var e = document.forms[0].elements, but I also can't get "if(isPosInt(ea['total' + i].value))" to work

Will this help? Watch for word-wrap.

<html>
<head>
<title>subtotal.htm</title>
<script type="text/javascript">
function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}
function isPosInt() {
// your code here!
return true;
}
function format(what) {
// your code here!
return what;
}
</script>
<style type="text/css">
..numb { text-align:right }
</style>
</head>
<body>
<form>
<br><input type="text" name="total" size="5" value="1" class="numb">
<br><input type="text" name="total" size="5" value="2" class="numb">
<br><input type="text" name="total" size="5" value="3" class="numb">
<br><input type="text" name="total" size="5" value="4" class="numb">
<br><input type="text" name="total" size="5" value="5" class="numb">
<br><input type="text" name="total" size="5" value="6" class="numb">
<br><input type="text" name="total" size="5" value="7" class="numb">
<br>=======
<br><input type="text" name="subtot" size="5" value="" class="numb">
<br><input type="button" value="Total!" onclick="subtotal()">
</form>
</body>
</html>

Obviously you should remove the values for all "total" fields.
Jul 23 '05 #2
On Wed, 22 Sep 2004 12:18:36 -0500, Abby Lee <ab*******@hotmail.com> wrote:
Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
Don't use parentheses for subscripting. Not only is it wrong, but only IE
will accept it.
ttot += document.forms[0].total(i).value;
This will act as string concatenation. Use:

ttot += (+document.forms[0].total[i].value);

The unary plus will force the value to a number.

I also suspect that 'total[i]' is incorrect. If the form control is named,
total<N>, where <N> is number, see later in this post. My suggestion will
apply here, too.

[snip]
And I do have a global varible elsewhere var e =
document.forms[0].elements,
It should probably be local.
but I also can't get "if(isPosInt(ea['total' + i].value))" to work


How is 'ea' set? In the previous thread, you used

var ea = document.forms[0].elements['elementName'];

What you need is:

var ea = document.forms[0].elements;

which you already have as the value of the variable, e.

If 'i' in the expression

ea['total' + i]

was 1, it would look up the control, 'total1' in the first form.

I probably didn't point you to the FAQ notes on accessing form controls. I
should have. The link below covers the basics. The resource linked at the
end of section goes into more detail.

<URL:http://jibbering.com/faq/#FAQ4_13>

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
> function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}


The code did not work fo me.
If I put an alert() message before the "var temp=document.forms[0].total
(i).value", I will see it when it trigger the code. But if I put an alert
after I will not see it.
Jul 23 '05 #4
"Abby Lee" <ab*******@hotmail.com> wrote in message
news:ui***************************@news.ks.uiuc.ed u...
function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}


The code did not work fo me.
If I put an alert() message before the "var temp=document.forms[0].total
(i).value", I will see it when it trigger the code. But if I put an alert
after I will not see it.


What's your browser?

I only tested it under IE5.5.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Frank | last post by:
I have a large form, that has text boxes of numbers in rows and columns. I need to sum the values in the columns, and put the total at the bottom of the column. But I also need to sum the values in...
11
by: Bobbak | last post by:
Hello All, I have these tables (lets call it ‘EmpCalls', ‘EmpOrders', and ‘Stats') that each contain the list of EmployeeIDs, I want to be able to create a Module in which I could call in my VB...
1
by: Steve | last post by:
I have looked through the newsgroup for an answer to this but haven't been able to find anything resembling my situation. What I want to do is relatively simple, I think. I have a crosstab...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
0
by: jy836 | last post by:
Hey all. I've created a DataGrid and bound it to a dataset, but I need to add several columns. The columns that I need to add have to be in the middle of the ones that are already there (as opposed...
2
by: Greg | last post by:
I was wondering if there is a simple way to present a totals column in a bound datagrid where the totals aren't actually persisted (in my case to an xml file). Ideally, when I populate the datagrid...
0
by: chrisbenoit06 | last post by:
Howdy...anyone able to assist me in adding the function to calculate the gross pay, and also the calculation and printing of the totals of hours and gross pay....thoughts? /* Purpose: This...
11
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
2
by: realtammie | last post by:
Hi, I got the following code from a post in these forums and the solution was provided by Bob Burrows. <% dim cn, rs set cn = Server.CreateObject("ADODB.Connection") set rs =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.