|
-
Apr 5th, 2002, 05:46 PM
#1
Thread Starter
Stuck in the 80s
Addition with JavaScript and Forms
I have a large number of forms on my webpage. How could I go about adding them up add displayin the result in another text box, as it's being typed?
-
Apr 5th, 2002, 06:16 PM
#2
Thread Starter
Stuck in the 80s
This is the closest I've gotten, but it just concenates (or whatever that word is) them together, so 1, 2, and 3, it would display 123:
Code:
lengthy code removed...
And what event should I call this with on the input area?
Last edited by The Hobo; Apr 6th, 2002 at 10:43 AM.
-
Apr 5th, 2002, 06:31 PM
#3
Member
In Java there's .intValue(), casting to an int, and int(some value), do those work?
-
Apr 5th, 2002, 06:34 PM
#4
Thread Starter
Stuck in the 80s
No...unless I'm doing it wrong.
-
Apr 5th, 2002, 06:44 PM
#5
Fanatic Member
You need to re-evaluate your parp directive
-
Apr 5th, 2002, 06:45 PM
#6
Thread Starter
Stuck in the 80s
Originally posted by Bonker Gudd
You need to re-evaluate your parp directive
Who?
-
Apr 6th, 2002, 10:42 AM
#7
Thread Starter
Stuck in the 80s
Alright, I got it working, but is there anyway I could simplify this code, maybe using some kind of loop?
Code:
function count() {
var a = parseInt(document.forms(0).box1.value) || 0;
var b = parseInt(document.forms(0).box2.value) || 0;
var c = parseInt(document.forms(0).box3.value) || 0;
var d = parseInt(document.forms(0).box4.value) || 0;
var e = parseInt(document.forms(0).box5.value) || 0;
var f = parseInt(document.forms(0).box6.value) || 0;
var g = parseInt(document.forms(0).box7.value) || 0;
var h = parseInt(document.forms(0).box8.value) || 0;
var i = parseInt(document.forms(0).box9.value) || 0;
var j = parseInt(document.forms(0).box10.value) || 0;
var k = parseInt(document.forms(0).box11.value) || 0;
var l = parseInt(document.forms(0).box12.value) || 0;
var m = parseInt(document.forms(0).box13.value) || 0;
document.forms(0).total1.value = a + b + c + d + e +
f + g + h + i + j + k + l + m;
var a = parseInt(document.forms(0).box14.value) || 0;
var b = parseInt(document.forms(0).box15.value) || 0;
var c = parseInt(document.forms(0).box16.value) || 0;
var d = parseInt(document.forms(0).box17.value) || 0;
var e = parseInt(document.forms(0).box18.value) || 0;
var f = parseInt(document.forms(0).box19.value) || 0;
var g = parseInt(document.forms(0).box20.value) || 0;
var h = parseInt(document.forms(0).box21.value) || 0;
var i = parseInt(document.forms(0).box22.value) || 0;
var j = parseInt(document.forms(0).box23.value) || 0;
var k = parseInt(document.forms(0).box24.value) || 0;
var l = parseInt(document.forms(0).box25.value) || 0;
var m = parseInt(document.forms(0).box26.value) || 0;
document.forms(0).total2.value = a + b + c + d + e +
f + g + h + i + j + k + l + m;
}
box1 through box13 are added in total1 and box14 through box26 are added in total2.
-
Apr 6th, 2002, 02:00 PM
#8
Fanatic Member
Hi,
Do you mean something like this...
Code:
function count(){
tot1=0;
for (i=1;i<14;i++){
if (document.forms(0).item('box'+i).value==""){
tot1=tot1+0;
}else{
tot1=tot1+parseInt(document.forms(0).item('box'+i).value);
}
}
document.forms(0).total1.value=tot1;
tot2=0;
for (i=14;i<27;i++){
if (document.forms(0).item('box'+i).value==""){
tot2=tot2+0;
}else{
tot2=tot2+parseInt(document.forms(0).item('box'+i).value);
}
}
document.forms(0).total2.value=tot2;
}
Its a looped version of your code.
-
Apr 7th, 2002, 08:46 PM
#9
Thread Starter
Stuck in the 80s
That's great. Thanks alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|