Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

calculate sum of many text inputs in javascript

Tue Dec 16, 2008 3:38 am

Ok, so here is the deal, I need some help with finalizing my code. I have the calculate sum function at the bottom but I guess it is written wrong? anyways, I can not get the total of my numbers to summeraze in my total box at the bottom. Here is my code. any help would be greatly appreciated!
Code:
<html>
<SCRIPT language = "JavaScript">
function calculate1() {
A = document.frmOne.txtFirstNumber1.value;
B = document.frmOne.txtSecondNumber1.value;
C = (A * B);
document.frmOne.txtThirdNumber1.value = C;
}

function calculate2() {
A = document.frmTwo.txtFirstNumber2.value;
B = document.frmTwo.txtSecondNumber2.value;
C = (A * B);
document.frmTwo.txtThirdNumber2.value = C;
}
</SCRIPT>
<BODY>
<P><FONT SIZE="+2">F.O.C.U.S. Life Insurance Calculator</FONT></P>
<table width="600" border="2" cellpadding="2">
<tr>
<td width="169"><font size="+3">F</font> - Funeral Expenses:</td>
<td width="186"><input type=TEXT name="input_A" size=25></td>
<td width="215"><font size="2">Flowers, Burial Plots, Caskets, Professional and Transportation Services, Food, Airfare, and

Others. Typically, the average funeral in the US is between $8,000 - $10,000. </font></td>
</tr>
<tr>
<td><font size="+3">O </font>- Other Debts:</td>
<td><input type=TEXT name="input_B" size=25></td>
<td><font size="2">Other Debts: Mortgages, Vehicle Loans, Credit Cards and other fiduciary responsibilities.</font></td>
</tr>
<tr>
<td><font size="+3">C </font>- College </td>
<td><FORM NAME = frmOne>
<p>Number of Children
<select name="txtFirstNumber1" id="txtFirstNumber1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</p>
<p>Type of College
<select name="txtSecondNumber1" id="txtSecondNumber1">
<option value="40000">Public/$10,000 per year</option>
<option value="100000">Private/$25,000 per year</option>
</select>
</p>
<P>
<input type = Button name = b1 value = "Add Numbers" onClick = "calculate1()">
<P>
  <input type = Text name="txtThirdNumber1" size = 25 value = "">

</FORM></td>


<td><table cellspacing="0" cellpadding="0">
<td colspan="8" height="32" width="515"><font size="2">The average cost of a (4) year public school is $10,000 per year and

$25,000 per year for private universities. You will want to choose the number of children you have here, if "0" leave

blank.</font></td>
</table></td>
</tr>
<tr>
<td><font size="+3">U</font> - Unforeseen Expenses </td>
<td><input type=TEXT name="input_A4" size=25></td>
<td> </td>
</tr>
<tr>
<td><font size="+3">S</font> - Salary </td>
<td><div align="center">
<FORM NAME = frmTwo>
<p align="left">Salary Needed Per Year</p>
<p align="left">
  <input type = Text name = "txtFirstNumber2" size = 15 value ="">
</p>
<p align="left">Number of Years</p>
<p align="left">
  <select name="txtSecondNumber2" id="txtsecondNumber2">
    <option value="5">5 Years</option>
    <option value="10">10 Years</option>
    <option value="15">15 Years</option>
    <option value="20">20 Years</option>
    <option value="25">25 Years</option>
    <option value="30">30 Years</option>
    <option value="35">35 Years</option>
    <option value="40">40 Years</option>
  </select>
</p>
<P align="left">
<input type = Button name = b12 value = "Add Numbers" onClick = "calculate2()">
<P align="left">
<input type = Text name="txtThirdNumber2" size = 25 value = "">
</FORM>
</div></td>
<td> </td>
</tr>
</table>
<table width="601" border="0">
  <tr>
    <td width="179">&nbsp;</td>
    <td width="382"><input type="button" value="Add Numbers" name="AddButton" onClick="CalculateSum(input_A.value, input_B.value, txtThirdNumber1.value,input_A4.value, txtThirdNumber2.value,)">
    <input type="button" value="Clear Fields" name="ClearButton" onClick="ClearForm(this.form)"></td>
    <td width="26">&nbsp;</td>
  </tr>
</table>
<table width="600" border="0">
  <tr>
    <td><div align="center"><font size="+2">F.O.C.U.S Total Life Insurance Needed=</font>
        <INPUT TYPE=TEXT NAME="Answer" SIZE=12>
    </div></td>
  </tr>
</table>
<P>&nbsp;</P>
<P>&nbsp;</P>
</BODY>
</HTML>



Last edited by DrRakha on Tue Dec 16, 2008 1:23 pm, edited 1 time in total.
Reason: change Title ,add [code] bbcode

Re: I know it, I'm a n00b. Need a little help here.

Tue Dec 16, 2008 1:20 pm

what is CalculateSum function ?? Where is its implemntaion !

Re: calculate sum of many text inputs in javascript

Wed Dec 17, 2008 12:14 am

i guess i dont know the answer to that. I need to add the following fields. input_A.value, input_B.value, txtThirdNumber1.value,input_A4.value, txtThirdNumber2.value

Like I said I am REALLY new at this and not sure how to continue. any help would be great.

Re: calculate sum of many text inputs in javascript

Wed Dec 17, 2008 7:56 pm

Code:
<html>
<SCRIPT language = "javascript">
function SumAll( value1, value2, value3, value4, value5)
{

  var sum=((((value1+value2)+value3)+value4)+value5);
Answer.value=sum;
}

function calculate1()
{
A = document.frmOne.txtFirstNumber1.value;
B = document.frmOne.txtSecondNumber1.value;
C = (A * B);
document.frmOne.txtThirdNumber1.value = C;
}

function calculate2() {
A = document.frmTwo.txtFirstNumber2.value;
B = document.frmTwo.txtSecondNumber2.value;
C = (A * B);
document.frmTwo.txtThirdNumber2.value = C;
}

</SCRIPT>
<BODY>
<P><FONT SIZE="+2">F.O.C.U.S. Life Insurance Calculator</FONT></P>
<table width="600" border="2" cellpadding="2">
<tr>
<td width="169"><font size="+3">F</font> - Funeral Expenses:</td>
<td width="186"><input type="TEXT" name="input_A" size="25"></td>
<td width="215"><font size="2">Flowers, Burial Plots, Caskets, Professional and Transportation Services, Food, Airfare, and

Others. Typically, the average funeral in the US is between $8,000 - $10,000. </font></td>
</tr>
<tr>
<td><font size="+3">O </font>- Other Debts:</td>
<td><input type=TEXT name="input_B" size=25></td>
<td><font size="2">Other Debts: Mortgages, Vehicle Loans, Credit Cards and other fiduciary responsibilities.</font></td>
</tr>
<tr>
<td><font size="+3">C </font>- College </td>
<td><FORM NAME = frmOne>
<p>Number of Children
<select name="txtFirstNumber1" id="txtFirstNumber1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</p>
<p>Type of College
<select name="txtSecondNumber1" id="txtSecondNumber1">
<option value="40000">Public/$10,000 per year</option>
<option value="100000">Private/$25,000 per year</option>
</select>
</p>
<P>
<input type="Button" name="b1" value = "Add Numbers" onClick = "javascript:calculate1()">
<P>
  <input type="Text" name="txtThirdNumber1" size="25" value="">

</FORM></td>


<td><table cellspacing="0" cellpadding="0">
<td colspan="8" height="32" width="515"><font size="2">The average cost of a (4) year public school is $10,000 per year and

$25,000 per year for private universities. You will want to choose the number of children you have here, if "0" leave

blank.</font></td>
</table></td>
</tr>
<tr>
<td><font size="+3">U</font> - Unforeseen Expenses </td>
<td><input type="TEXT" name="input_A4" size="25"></td>
<td> </td>
</tr>
<tr>
<td><font size="+3">S</font> - Salary </td>
<td><div align="center">
<FORM NAME ="frmTwo">
<p align="left">Salary Needed Per Year</p>
<p align="left">
  <input type="Text" name="txtFirstNumber2" size="15" value ="">
</p>
<p align="left">Number of Years</p>
<p align="left">
  <select name="txtSecondNumber2" id="txtsecondNumber2">
    <option value="5">5 Years</option>
    <option value="10">10 Years</option>
    <option value="15">15 Years</option>
    <option value="20">20 Years</option>
    <option value="25">25 Years</option>
    <option value="30">30 Years</option>
    <option value="35">35 Years</option>
    <option value="40">40 Years</option>
  </select>
</p>
<P align="left">
<input type="Button" name="b12" value="Add Numbers" onClick="calculate2()">
<P align="left">
<input type="Text" name="txtThirdNumber2" size="25" value="">
</FORM>
</div></td>
<td> </td>
</tr>
</table>
<table width="601" border="0">
  <tr>
    <td width="179">&nbsp;</td>
    <td width="382"><input type="button" value="Add Numbers" name="AddButton" onClick="
   SumAll(input_A.value, input_B.value, frmOne.txtThirdNumber1.value,input_A4.value, frmTwo.txtThirdNumber2.value)">
    <input type="button" value="Clear Fields" name="ClearButton" onClick=""></td>
    <td width="26">&nbsp;</td>
  </tr>
</table>
<table width="600" border="0">
  <tr>
    <td><div align="center"><font size="+2">F.O.C.U.S Total Life Insurance Needed=</font>
        <INPUT TYPE="TEXT" NAME="Answer" SIZE=12>
    </div></td>
  </tr>
</table>
<P>&nbsp;</P>
<P>&nbsp;</P>
</BODY>
</HTML>

Solved for you :)

Post a reply
  Related Posts  to : calculate sum of many text inputs in javascript
 Need help with JavaScript function calculate the total pric!     -  
 Typing Status Bar Text javascript code     -  
 Inputs in HTML5     -  
 Able to Copy Text from Uneditable Text Boxes(JTextfields)     -  
 Calculate euclidean distances     -  
 calculate speed of a car in javaCV     -  
 Calculate the sum of values in an array     -  
 Calculate score of the student     -  
 How to Calculate Volume of a Rectangle     -  
 Calculate process time     -  

Topic Tags

JavaScript Variables