adding numbers together
Hi Does anyone know what i'm doing wrong here. Instead of adding my numbers together its putting them next to each other?
This is the form...
totalincome = (myIncome1 + myIncome2 + myIncome3 + myIncome4);
form.Tincome.value = totalincome;
myIncome1 = inc1.value;
myIncome2 = inc2.value;
myIncome3 = inc3.value;
myIncome4 = inc4.value;
form.myIncome1.value = "";
form.myIncome2.value = "";
form.myIncome3.value = "";
form.myIncome4.value = "";
form.Tincome.value = "";
<div><p>Pay <input name="myIncome1" type="text" ONCHANGE="SetMyIncome1(this)"/></p>
<p>Benefits <input name="myIncome2" type="text" ONCHANGE="SetMyIncome2(this)"/></p>
<p>Pension <input name="myIncome3" type="text" ONCHANGE="SetMyIncome3(this)"/></p>
<p>Other income <input name="myIncome4" type="text" ONCHANGE="SetMyIncome4(this)"/></p>
<p><INPUT TYPE=BUTTON ONCLICK="HowMany(this.form)" VALUE="Calculate Income"><INPUT TYPE=BUTTON VALUE=" Reset " onClick="ClearForm(this.form)"></p>
<p>Total <input name="Tincome" type="text" value="" /></p></div></form
Does anyone know how to set the value to 0 if they don't enter a value? I get NaN if they miss a box but dont want to put a check form on as they dont have to fill out all the boxes.
totalincome = parseFloat(form.myIncome1.value)+parseFloat(form.myIncome2.value)+parseFloat(form.myIncome3.value)+parseFloat(form.myIncome4.value);
+ is the default string concatenation function, unless you specifically cast your vars as number types, + will treat them as if they are strings.
Michael G. Workman
thanks, it took me a while but I figured it out. I'm getting used to it a bit now but its very complicated!! :-)