Solved

Total of calculating form

Posted on 2004-04-23
7
275 Views
Last Modified: 2012-06-21
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
Comment
Question by:PeterGHill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 10897894
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 10897903
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
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10897910
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!

 
LVL 31

Accepted Solution

by:
GwynforWeb earned 500 total points
ID: 10897975
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
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10898022
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
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10898025
thx for the points GfW
0
 

Author Comment

by:PeterGHill
ID: 10898058
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question