Link to home
Start Free TrialLog in
Avatar of PeterGHill
PeterGHill

asked on

Total of calculating form

Dear All,

Firstly I know nothing of Javascript so I hope I am clear.

I have simle for that works fine but I can not total the sum of the three totaling fields. Also is it possibe for me to subsitite the following for the javascript in the header so that I can remove the current field 1 and its default value of 5 so that I can programme specific values for each input to be caluclated against?
EG: field3.value = field1.value * 7; //?

<script language="javascript">
function doCalculate()
{
with(document.myForm)
{
field3.value = field1.value * field2.value;
field4.value = field1.value * field2.value;
field5.value = field1.value * field2.value;
total = field3.value + field4.value + field5.value; //this is the field I need help with...
}
}
</script>


Main HTM FORM

<form name="myForm">
<input name="field1" type="text" value="5" size="3">
<input name="field2" type="text" size="3">
<input name="field3" type="text" size="3" maxlength="4">
<input name="field4" type="text" size="3">
<input name="field5" type="text" size="3">
<input name="button" type="button" onClick="doCalculate()" value="Calculate">
TOTAL<input name="total" type="text" size="4">
</form>
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Perhaps like this:

<script language="javascript">
function doCalculate(theField){
  with(theField.form) {
    field3.value = field1.value * field2.value;
    field4.value = field1.value * field2.value;
    field5.value = field1.value * field2.value;
    total.value = field3.value * 1 + field4.value * 1 + field5.value * 1;
  }
}
</script>

<form name="myForm">
<input name="field1" type="text" value="5" size="3" onKeyUp="doCalculate(this)">
<input name="field2" type="text" size="3" onKeyUp="doCalculate(this)">
<input name="field3" type="text" size="3" maxlength="4" onKeyUp="doCalculate(this)">
<input name="field4" type="text" size="3" onKeyUp="doCalculate(this)">
<input name="field5" type="text" size="3" onKeyUp="doCalculate(this)">
TOTAL<input name="total" type="text" size="4" onFocus="this.blur()">
</form>

Or perhaps better this:

<script language="javascript">
function doCalculate(theField){
  with(theField.form) {
    field3.value = field1.value * field2.value;
    field4.value = field1.value * field2.value;
    field5.value = field1.value * field2.value;
    total.value = field3.value * 1 + field4.value * 1 + field5.value * 1;
  }
}
</script>

<form name="myForm">
<input name="field1" type="text" value="5" size="3" onKeyUp="doCalculate(this)">
<input name="field2" type="text" size="3" onKeyUp="doCalculate(this)">
<input name="field3" type="text" size="3" onFocus="this.blur()">
<input name="field4" type="text" size="3" onFocus="this.blur()">
<input name="field5" type="text" size="3" onFocus="this.blur()">
TOTAL<input name="total" type="text" size="4" onFocus="this.blur()">
</form>

Use this

<script language="javascript">
function doCalculate()
{
with(document.myForm)
{
field3.value = field1.value * field2.value;
field4.value = field1.value * field2.value;
field5.value = field1.value * field2.value;
total.value = 1*field3.value + 1*field4.value + 1*field5.value;
}
}
</script>


Main HTM FORM

<form name="myForm">
<input name="field1" type="text" value="5" size="3">
<input name="field2" type="text" size="3">
<input name="field3" type="text" size="3" maxlength="4">
<input name="field4" type="text" size="3">
<input name="field5" type="text" size="3">
<input name="button" type="button" onClick="doCalculate()" value="Calculate">
TOTAL<input name="total" type="text" size="4">
</form>
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Changing the form to this in the above is cool as well

<form name="myForm">
<input name="field2" type="text" size="3" onKeyup="doCalculate()">
<input name="field3" type="text" size="3" maxlength="4">
<input name="field4" type="text" size="3">
<input name="field5" type="text" size="3">
TOTAL<input name="total" type="text" size="4">
</form>
thx for the points GfW
Avatar of PeterGHill
PeterGHill

ASKER

thanks for the neat very prompt answer, clients site now going live...
onto the next one!

pete