473,805 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with form calculation

2 New Member
I was searching ALL over for a javascript to just add up a column of numbers and I found this one here in an old question.

It seemed to work great for my purposes EXCEPT it stops adding at four and I have more input fields than that.

I tried adjusting the
for (var i=1;i<=4;i++) { to
for (var i=1;i<=12;i++) {
to reflect the number of fields and thinking that was the issue but I got an error message about form elements being invalid.

If anyone has an idea of what I might try i would appreciate it.

<html>
<head>
<title></title>
<SCRIPT LANGUAGE="JavaS cript">
<!--


function sumIt() {
var f = document.forms["myForm"]
var s = 0
for (var i=1;i<=4;i++) {
var v = f.elements["Val"+i].value
v = parseFloat(v)
if (isNaN(v)) continue
v = Math.abs(v)
s+=v
}
f.elements["exptotal"].value = s

}



//-->
</SCRIPT>
</head>
<body>
<form name="myForm">
<p>
one: <input type=text name="Val1" onchange="sumIt ()">
</p>
<p>
two: <input type=text name="Val2" onchange="sumIt ()">
</p>
<p>
three: <input type=text name="Val3" onchange="sumIt ()">
</p>
<p>
four: <input type=text name="Val4" onchange="sumIt ()">
</p>
<p>
Suma:<input type="text" READONLY class="disabled " name="exptotal" size="20"
value="0.00">
</p>


</form>
</body>
</html>
Jul 4 '07 #1
2 1802
pbmods
5,821 Recognized Expert Expert
Heya, Pablo. Welcome to TSDN!

Try this instead:

Expand|Select|Wrap|Line Numbers
  1. function sumIt() {
  2.     var f = document.forms["myForm"];
  3.     var s = 0;
  4.     var i = 0;
  5.     var v;
  6.     while(v = f.elements['Val' + (++i)]) {
  7.         if(isNaN(v = parseFloat(v.value)))
  8.             continue;
  9.         s += Math.abs(v);
  10.     }
  11.     f.elements["exptotal"].value = s;
  12. }
This version of sumIt() will use every single input, as long as they are all numbered in order, starting with 'Val1'.
Jul 4 '07 #2
Uncle pablo
2 New Member
Hey thanks PB...
It works like a champ !!
Jul 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1397
by: Umesh | last post by:
Hi I am developing a windows application. This contains a Data grid which will populate data from a data table in a Dataset. The application will do some calculation and store the result in the Data table. After the calculation, the result stored in the data table will display in the data grid. When number of rows need to populate in the data table after calculation is around 16000, application will take around 30 seconds to do the...
3
2573
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
9
3162
by: chris vettese | last post by:
On my subform I have a field in the footer that totals the value of a field. On the main form I have referenced this field. I'm using this field in a calculation on my main form. The problem occurs when there are no records in the subform. The field on the main form that references the subform total field returns #Error. How can I make this field show the total when there are records and 0 when there are no records??? Thank you,...
1
2278
by: cdelaney | last post by:
I have a form that I created a calculation on using 2003. The calculation works exactly like I want it to but ONLY on the first and last record. The calculation does not work/exist on records in between .. I have checked many of the properties to see what got set unintentionally that might make only the first and last record show the values. Has anyone experienced this? I'm baffled.
13
3264
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
4
3766
by: vg-mail | last post by:
Hello all, I have identical design for form and report but I am getting calculation error on form and everything is OK on report. The form and report are build up on SQL statement. The calculation is very simple. The calculation is done in an underling query if I can call it a query or I should call it a SQL statement. It looks like a query but it is not saved as the query. The calculation in that query is very simple - ExtendedPrice:*....
7
1468
by: ChrisM | last post by:
Hi, I'm trying to get my head round Async. procedure calls. I have the following code: delegate double GetLabourWIPDelegate(DateTime reqDate); .... GetLabourWIPDelegate getLabourWIPDelegate = new GetLabourWIPDelegate(GetLabourWIPOnDate);
10
2544
by: 60325 | last post by:
This is the page where I collect the data in drop-down boxes with values of 1-10 and send it to a submitted page to do calculations. Example: Employee1 TeamScore(1-10) Employee2 TeamScore(1-10) Employee3 TeamScore(1-10) Employee4 TeamScore(1-10) Then I submit this page with all the values in TeamScore for every employee and I want to perform a calculation based on the values in the drop-down and a
2
2667
by: semutmerah | last post by:
hi.. my english maybe wrong.. but hope somebody can understand me :) I did created 3 forms. "MainForm" , "CalForm" , "WaitForm". On "MainForm", I did put a command button called "Run". On "CalForm", there were some unbound "text box" which have some calculation
3
1721
by: FrozenDude | last post by:
I've implemented Allen Browne's Audit Log and it works quite well (thanks). However, my form has a series of calculated fields that fire on the form OnCurrent event. The calculation causes a record to written to the Audit Log. This means that a log entry is created even if users are only scrolling through the records. Is there a way to block a log entry if the only form change is an auto calculation? Thanks in advance.
0
10609
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
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 we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.