• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Javascript Calculation

Hello,

I have a or with a number of input options -

<form action="" method="post" name="form1" id="form1" onsubmit="return validateform1(this)">
<h3>Complete Survey Now</h3>
<ul>
    <li>
      <label>Retainer Value :</label>
      <input name="Locality" type="hidden" id="Locality" value="<%=(RSMemberLocality.Fields.Item("Locality").Value)%>" />        <input name="Retainerinput" type="hidden" id="Retainerinput" value="yes" />        
      <input name="RetainerValue" type="text" id="RetainerValue" maxlength="8" onBlur="this.value = formatField(this)" />
      </li>
    <li>
      <label>Fee Commission :</label>
      <input name="FeeCommission" type="text" id="FeeCommission" maxlength="8" onBlur="this.value = formatField(this)" />
      </li>
    <li>
      <label>Total :</label>
      <input name="TotalRetainer" type="text" id="TotalRetainer" maxlength="8" onBlur="this.value = formatField(this)" />
      </li>
    <li>
      <label>Date of Review </label>
      <input type="text" id="ReviewDate" name="ReviewDate" />

      </li>
    <li>
      <label>Comments</label>
      <div class="retainerholdertextarea"><textarea name="Comments" cols="36" rows="5" id="Comments"></textarea></div>
      <input type="image" class="submitbutton" src="png/submit.png">
      </li>
</ul>
</form>

Open in new window


I would like it if

TotalRetainer was a calculated value based on the input into either or both of -

RetainerValue
FeeCommission

So if only FeeCommision is entered the value in TotalRetainer  is the same as the value in FeeCommission, OR if both are entered the value is the Sum of both.

The calculation needs to happen on blur of either input.

And nost importantly, the input needsto be updateable, so if the sum of both is 10, but more parameters are involved and the actual total is 12, the user can over write the javascript calculated value...

Appreciate any help you can offer - Many thanks

0
garethtnash
Asked:
garethtnash
  • 3
2 Solutions
 
garethtnashAuthor Commented:
Hi - slight update -

I've tried -

<script>
<!--
function calc(RetainerValue,FeeCommission,SUM) {
  var one = document.getElementById(RetainerValue).value;
  var two = document.getElementById(FeeCommission).value;
  document.getElementById(SUM).value = one + two;
}
//-->
</script>

AND

      <td><input name="Locality" type="hidden" id="Locality" value="<%=(RSMemberLocality.Fields.Item("Locality").Value)%>" />        <input name="Retainerinput" type="hidden" id="Retainerinput" value="yes" />        
      <input name="RetainerValue" type="text" id="RetainerValue" maxlength="8" onBlur="this.value = formatField(this);calc('one','two','TotalRetainer')" /></td>
      </tr>
    <tr class="altrow">
      <td class="labelcolumn">Fee Commission :</td>
      <td><input name="FeeCommission" type="text" id="FeeCommission" maxlength="8" onBlur="this.value = formatField(this);calc('one','two','TotalRetainer')" /></td>
      </tr>
    <tr>
      <td class="labelcolumn">Total :</td>
      <td><input name="TotalRetainer" type="text" id="TotalRetainer" maxlength="8" onBlur="this.value = formatField(this)" /></td>


But no joy -

Thanks
0
 
COBOLdinosaurCommented:
The values being returned are strings so the need to be converted. Try:
function calc(RetainerValue,FeeCommission,SUM) {
  var one = parseFloatdocument.getElementById(RetainerValue).value);
  var two = parseFloat(document.getElementById(FeeCommission).value);
  document.getElementById(SUM).value = one + two;
}

Open in new window

0
 
garethtnashAuthor Commented:
Hi COBOLdinosaur,

Thanks for the post, but sadly no joy..

Thanks
0
 
Pratima PharandeCommented:
try this

add it to your code this is only part of code
<HTML>
<HEAD>
<TITLE>Calculator</TITLE>
<SCRIPT type="text/javascript">
function calc(RetainerValue,FeeCommission,SUM) {
  var one =  parseFloat( document.getElementById(RetainerValue).value);
  var two = parseFloat(document.getElementById(FeeCommission).value); 
  document.getElementById(SUM).value = one + two;
}

</SCRIPT>
</HEAD>

<BODY>


<form action="" method="post" name="form1" id="form1" onsubmit="return validateform1(this)">
<h3>Complete Survey Now</h3>
<table>
<tr>
<td>
<label>Retainer Value :</label>

<input name="RetainerValue" type="text" id="RetainerValue" maxlength="8" onBlur="calc('RetainerValue','FeeCommission','TotalRetainer')" /></td>
      </tr>
    <tr class="altrow">
      <td class="labelcolumn">Fee Commission :</td>
      <td><input name="FeeCommission" type="text" id="FeeCommission" maxlength="8" onBlur="calc('RetainerValue','FeeCommission','TotalRetainer')" /></td>
      </tr>
    <tr>
      <td class="labelcolumn">Total :</td>
      <td><input name="TotalRetainer" type="text" id="TotalRetainer" maxlength="8" onBlur="this.value = formatField(this)" /></td>

</tr>
</table>
</form>


</BODY>
</HTML>

Open in new window

0
 
garethtnashAuthor Commented:
Thanks Guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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