473,378 Members | 1,415 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,378 software developers and data experts.

Need help with form calculation

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="JavaScript">
<!--


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 1778
pbmods
5,821 Expert 4TB
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
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
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...
3
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...
9
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...
1
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...
13
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...
4
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...
7
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...
10
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 ...
2
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". ...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.