Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

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>
0
PeterGHill
Asked:
PeterGHill
  • 4
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
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>

0
 
ZvonkoSystems architectCommented:
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>

0
 
GwynforWebCommented:
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>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GwynforWebCommented:
or this, it multiplies by 5, 8 and 7 ( fact[3]=5  fact[4]=8  fact[5]=7 )

<script language="javascript">

fact=new Array(0,0,0,5,8,7)

function doCalculate()
{
with(document.myForm)
{
field3.value = fact[3] * field2.value;
field4.value = fact[4] * field2.value;
field5.value = fact[5] * field2.value;
total.value = 1*field3.value + 1*field4.value + 1*field5.value;
}
}
</script>


Main HTM FORM

<form name="myForm">
<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>
0
 
GwynforWebCommented:
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>
0
 
GwynforWebCommented:
thx for the points GfW
0
 
PeterGHillAuthor Commented:
thanks for the neat very prompt answer, clients site now going live...
onto the next one!

pete
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now