Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Total of calculating form

Posted on 2004-04-23
7
Medium Priority
?
279 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 31

Accepted Solution

by:
GwynforWeb earned 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

670 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