473,503 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with loop to add numbers

Hi there,

I'm trying to add the values of a number of form fields and to get a
result at the end. It must loop and be able to dynamically update the
result of calculation. I have attached the code i have so far.

Thanks

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script language="JavaScript1.2" type="text/JavaScript">

function calculate(currid)
{
var rownumber = "";
var Qnt;
var Qnt_Total;

rownumber = currid.substring(currid.indexOf("_", currid) + 1);

//DEBUGGING
//alert(rownumber);
//return false;

Qnt = document.getElementById("Quantity_" + rownumber).value;

for(var j = 0 ; j <= document.CaptureForm.elements.length ; j++)
{
cnt = j + 1;
//if(eval(document.CaptureForm.elements[j].id) == "[object]")
if(document.CaptureForm.elements[j].id ==
"Quantity_"+String(cnt))
{
if(document.CaptureForm.elements[j].value != "")
Qnt_Total += Number(document.CaptureForm.elements[j].value);
}
}

document.CaptureForm.Totals1.value = Qnt_Total;
}

</script>

</head>

<body>
<form action="" method="post" name="CaptureForm">

<SPAN id="Quantity_Label" style="LEFT: 518px; WIDTH: 62px; COLOR:
#000000;POSITION: absolute; TOP: 186px; HEIGHT: 13px; BACKGROUND-COLOR:
transparent; FONT-FAMILY:MS Sans Serif; FONT-SIZE:8pt;">Quantity</SPAN>
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 200; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity" id="Quantity_1" value="<?php echo
$row_Recordset1['Quantity']; ?>" onBlur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 226; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity2" id="Quantity_2" value="<?php echo
$row_Recordset1['Quantity2']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 252; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity3" id="Quantity_3" value="<?php echo
$row_Recordset1['Quantity3']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 279; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity4" id="Quantity_4" value="<?php echo
$row_Recordset1['Quantity4']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 305; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity5" id="Quantity_5" value="<?php echo
$row_Recordset1['Quantity5']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 331; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity6" id="Quantity_6" value="<?php echo
$row_Recordset1['Quantity6']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 358; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity7" id="Quantity_7" value="<?php echo
$row_Recordset1['Quantity7']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 384; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity8" id="Quantity_8" value="<?php echo
$row_Recordset1['Quantity8']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 410; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity9" id="Quantity_9" value="<?php echo
$row_Recordset1['Quantity9']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 436; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity10" id="Quantity_10" value="<?php echo
$row_Recordset1['Quantity10']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 463; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity11" id="Quantity_11" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 489; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity12" id="Quantity_12" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 493; text-align: right; WIDTH:
100px; COLOR: #000000;POSITION: absolute; TOP: 518; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Totals1" id="Totals1" value="<?php echo
$row_Recordset1['Totals1']; ?>">
</form>

</body>
</html>

Jul 10 '06 #1
3 2398
Hi Lawrence,

Do you have a question? Besides, you can't use javascript: in an
eventhandler attribute (in your case onblur), because it already expects
that. javascript: is only used as an URL.

Vincent


la*******@lithotech.co.za wrote:
Hi there,

I'm trying to add the values of a number of form fields and to get a
result at the end. It must loop and be able to dynamically update the
result of calculation. I have attached the code i have so far.

Thanks

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script language="JavaScript1.2" type="text/JavaScript">

function calculate(currid)
{
var rownumber = "";
var Qnt;
var Qnt_Total;

rownumber = currid.substring(currid.indexOf("_", currid) + 1);

//DEBUGGING
//alert(rownumber);
//return false;

Qnt = document.getElementById("Quantity_" + rownumber).value;

for(var j = 0 ; j <= document.CaptureForm.elements.length ; j++)
{
cnt = j + 1;
//if(eval(document.CaptureForm.elements[j].id) == "[object]")
if(document.CaptureForm.elements[j].id ==
"Quantity_"+String(cnt))
{
if(document.CaptureForm.elements[j].value != "")
Qnt_Total += Number(document.CaptureForm.elements[j].value);
}
}

document.CaptureForm.Totals1.value = Qnt_Total;
}

</script>

</head>

<body>
<form action="" method="post" name="CaptureForm">

<SPAN id="Quantity_Label" style="LEFT: 518px; WIDTH: 62px; COLOR:
#000000;POSITION: absolute; TOP: 186px; HEIGHT: 13px; BACKGROUND-COLOR:
transparent; FONT-FAMILY:MS Sans Serif; FONT-SIZE:8pt;">Quantity</SPAN>
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 200; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity" id="Quantity_1" value="<?php echo
$row_Recordset1['Quantity']; ?>" onBlur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 226; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity2" id="Quantity_2" value="<?php echo
$row_Recordset1['Quantity2']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 252; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity3" id="Quantity_3" value="<?php echo
$row_Recordset1['Quantity3']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 279; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity4" id="Quantity_4" value="<?php echo
$row_Recordset1['Quantity4']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 305; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity5" id="Quantity_5" value="<?php echo
$row_Recordset1['Quantity5']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 331; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity6" id="Quantity_6" value="<?php echo
$row_Recordset1['Quantity6']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 358; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity7" id="Quantity_7" value="<?php echo
$row_Recordset1['Quantity7']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 384; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity8" id="Quantity_8" value="<?php echo
$row_Recordset1['Quantity8']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 410; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity9" id="Quantity_9" value="<?php echo
$row_Recordset1['Quantity9']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 436; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity10" id="Quantity_10" value="<?php echo
$row_Recordset1['Quantity10']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 463; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity11" id="Quantity_11" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 489; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity12" id="Quantity_12" value="<?php echo
$row_Recordset1['Quantity11']; ?>" onblur="javascript:
calculate(this.id);">
<INPUT type="text" style="LEFT: 493; text-align: right; WIDTH:
100px; COLOR: #000000;POSITION: absolute; TOP: 518; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Totals1" id="Totals1" value="<?php echo
$row_Recordset1['Totals1']; ?>">
</form>

</body>
</html>
Jul 10 '06 #2
la*******@lithotech.co.za writes:
I'm trying to add the values of a number of form fields and to get a
result at the end. It must loop and be able to dynamically update the
result of calculation. I have attached the code i have so far.
<script language="JavaScript1.2" type="text/JavaScript">
Don't use the language attribute. If you do use the langauge
attribute, make sure *not* to specify version 1.2. That particular
version of Javascript was only the most current one in Nescape
Navigator 4.0 through 4.06. It had slightly different behavior
from earlier and later versions, and was probably replaced for
that reason. Specifying version 1.2 will make Netscape and Mozilla
browsers use that slightly different behavior, whereas other
browsers will use the behavior of the more recent versions
of Javascript.

function calculate(currid)
{
var rownumber = "";
The indentation makes it hard to read and comment on the code, so I've
prettyprinted it:
function calculate(currid) {
I assume currid is the id of an input element.
var rownumber = "";
var Qnt;
var Qnt_Total;
rownumber = currid.substring(currid.indexOf("_", currid) + 1);
Qnt = (document.getElementById("Quantity_" + rownumber)).value;
Isn't "Quantity_"+rownumber always the same as currid?

You are not using "Qnt" anywhere, so both rownumber and Qnt can be
omitted.

for (var j = 0;j <= document.CaptureForm.elements.length;j++) {
"<=" should be "<"
cnt = j + 1;
if (document.CaptureForm.elements[j].id == "Quantity_" + String(cnt))
You are expecting the Quantity_x's to come in a fixed order. They
probably do, but it's not necessary for computing the sum.
{
if (document.CaptureForm.elements[j].value != "")
Qnt_Total += Number(document.CaptureForm.elements[j].value);
Qnt_Total was never initialized, so it initially contained undefined.
Adding numbers to that gives NaN.
}
}
document.CaptureForm.Totals1.value = Qnt_Total;
}
Try this instead:

<script type="text/javascript">
function calculate(inputId) {
var totalQuantity = 0;
var elems = document.forms['CaptureForm'].elements;
for(var i = 0; i < elems.length; i++) {
var elem = elems[i];
if (elem.id.matches(/^Quantity_\d+$/)) {
totalQuantity += Number(elem.value):
}
}
elems['Totals1'].value = tatalQuantity;
}
</script>

<form action="" method="post" name="CaptureForm">
Is the form intended for submission? In that case, you should state
the URL, and not just rely on the browser posting to the same URL as
itself (it should do that, but some browsers didn't).
Some comments on the HTML:
<SPAN id="Quantity_Label" style="LEFT: 518px; WIDTH: 62px; COLOR:
#000000;POSITION: absolute; TOP: 186px; HEIGHT: 13px; BACKGROUND-COLOR:
transparent; FONT-FAMILY:MS Sans Serif; FONT-SIZE:8pt;">Quantity</SPAN>
You should look into using a stylesheet, so you don't specify this
huge style attribute on every span (easier to read and easier to
maintain, since the common styles are specified in just one
place).

You might also want to use the <labelelement for labels:
<label id="Quantity_Label" for="Quantity_1">Quantity</label>

It looks like auto-generated code by a less-than-brilliant HTML
authoring program :)
<INPUT type="text" style="LEFT: 506; text-align: right; WIDTH: 86px;
COLOR: #000000;POSITION: absolute; TOP: 200; HEIGHT: 19px;
BACKGROUND-COLOR: transparent; FONT-FAMILY:MS Sans Serif;
FONT-SIZE:8pt;" name="Quantity" id="Quantity_1" value="<?php echo
Your "name" attribute is inconsistent. To match the other lines,
it would be "Quantity1".
$row_Recordset1['Quantity']; ?>" onBlur="javascript:
calculate(this.id);">
You are including php-code here. That does not help us help you, since
we annot know exactly what HTML code the browser sees. I'll assume
that the PHP code just inserts a number.

The onblur attribute starts with "javascript:". That's unnecessary, and
only coincidentally not an error. The Javascript parser sees it as
a label that just happens to be called "javascript", but could equally
well be called "arglebargle". (However, IE parses it as specifying
that the attribute should be parsed as Javascript, which is only
necessary if the page's general scripting language is something else,
like vbscript).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 10 '06 #3
Vincent van Beveren wrote:
<snip>
>... Besides, you can't use javascript: in an eventhandler attribute (in
your case onblur), because it already expects that.
The values for intrinsic event handling attributes can start with -
javascript: -. In that context it would be a label by javascript syntax
rules. Putting a label in that context is almost always worthless, and
as a label - javascript - is a dangerous choice as IE browsers attach
additional meaning to the construct in a way that will not result in a
label, but it still syntactically valid, mostly harmless and allowed.
javascript: is only used as an URL.
<snip>

The javascript pseudo protocol (which is a different thing from a label
in an intrinsic event attribute value) should almost never be used at
all. ('almost' meaning that there are maybe two contexts at most where
its use might be appropriate, neither or which are common).
la*******@lithotech.co.za wrote:
<snip>

Please do not top-post to comp.lang.javascirpt, and do trim the
material that you do quote to only that which is necessary to maintain
the context of your response.

Richard.

Jul 10 '06 #4

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

Similar topics

4
1533
by: Grant Austin | last post by:
Hi all, This is my first time posting so please bear with me. I would like some criticism of some elementary C++ work I'm doing because it's been a long long long time and I'm not sure if I'm...
2
321
by: Kramer55 | last post by:
I started teaching myself programming over the summer, but have not had the time. I recently began to start my programming again, but realized i couldnt even do a simple problem create a...
1
2295
by: AJ Smith | last post by:
To Whom It May Concern: I have the following code: #include <cctype> //Provides isdigit #include <cstdlib> //Provides EXIT_SUCCESS #include <cstring> //Provides strchr #include <iostream>...
4
1629
by: UMstudent | last post by:
I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator...
1
6112
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
10
4341
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users...
2
1399
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
15
2721
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
5
13693
by: AB3004 | last post by:
Hey there, Just wanted to say Hi. - I'm new on here & have drifted here as I'm having a minor problem I was hoping someone could assist ...? (for some reason I couldn't post this directly into the...
0
7205
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
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7008
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
7467
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...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.