Solved

Total of calculating form

Posted on 2004-04-23
7
270 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
  • 4
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Accepted Solution

by:
GwynforWeb earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thx for the points GfW
0
 

Author Comment

by:PeterGHill
Comment Utility
thanks for the neat very prompt answer, clients site now going live...
onto the next one!

pete
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now