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?
Printable View
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?
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:
And what event should I call this with on the input area?Code:lengthy code removed...
In Java there's .intValue(), casting to an int, and int(some value), do those work?
No...unless I'm doing it wrong. :confused:
You need to re-evaluate your parp directive :mad:
Who?Quote:
Originally posted by Bonker Gudd
You need to re-evaluate your parp directive :mad:
Alright, I got it working, but is there anyway I could simplify this code, maybe using some kind of loop?
box1 through box13 are added in total1 and box14 through box26 are added in total2.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;
}
Hi,
Do you mean something like this...
Its a looped version of your code.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;
}
That's great. Thanks alot :)