Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am doing a conditional statement in javascript. When the user enters a value in the input box "Collect", its value together with the value from input box "CurBal" will perform an addition and put the answer in the input box named "Balance".

If the user enters a value in the input box "Deposit", its value will be substracted from the "CurBal" and the difference will also be reflected (supposedly) in the "Balance"

I have this code:

JavaScript
var collect = parseFloat(document.getElementById("Collect").value) ||0;
var dep     = parseFloat(document.getElementById("Deposit").value) ||0;
var curr    = parseFloat(document.getElementById("CurBal").value) ||0;
var bal     = document.getElementById("Balance");
						
	if (collect.value =! 0)
	{
          bal.value = (collect + curr).toFixed(2);
	}
						
	if (dep.value =! 0)
	{
	  bal.value = (curr - dep).toFixed(2);
	} 


The HTML:
HTML
<tr>
   <td>  Collections: </td>
    <td>   <input  önchange="addNumbers()" type="text" id="Collect" name="Collect" value="" /> </td>
  </tr>
  
  <tr>
    <td>  Deposits: </td>
    <td>  <input  önchange="addNumbers()" type="text" id="Deposit" name="Deposit" value="" /> </td>
  </tr>
  
  <tr>
    <td>  Current Balance: </td>
    <td>  <input  önchange="addNumbers()" type="text" id="CurBal" name= "CurBal" value="<?php echo $all['balance']; ?>" readonly="readOnly" /> </td>
  </tr>

  <tr>
    <td>  Balance: </td>
    <td>  <input type="text" id="Balance" name="Balance" value="" readonly="readOnly" /> </td>
</tr>


But, its just performing the 2nd condition correctly. If I entered a value in the input box "Collect", it isn't adding the value from "CurBal", its just copying the value from "CurBal".

What should be removed or added or changed? Thank you for the help

Additional info: The user can only EITHER enter a value to "Collect" OR "Deposit".
Posted
Updated 12-Mar-14 20:22pm
v5

I modified your code as given below. Hope may help you.

C#
function addNumbers() {

           var collect = parseFloat(document.getElementById("Collect").value) || 0;
           var dep = parseFloat(document.getElementById("Deposit").value) || 0;
           var curr = parseFloat(document.getElementById("CurBal").value);

           if (collect.value != 0) {
               var addbal = collect + curr;
               document.getElementById("Balance").value = addbal;
           }
       }

       function subNumbers() {

           var collect = parseFloat(document.getElementById("Collect").value) || 0;
           var dep = parseFloat(document.getElementById("Deposit").value) || 0;
           var curr = parseFloat(document.getElementById("CurBal").value);

           if (collect.value != 0) {
               var subbal = collect - dep;
               document.getElementById("Balance").value = subbal;
           }
       }


HTML code:
<tr>
           <td>Collections: </td>
           <td>
               <input  type="text" id="Collect" name="Collect"  />
           </td>
       </tr>
       <tr>
           <td>Deposits: </td>
           <td>
               <input  onchange="subNumbers()"  type="text" id="Deposit"          name="Deposit" />
           </td>
       </tr>
       <tr>
           <td>Current Balance: </td>
           <td>
               <input onchange="addNumbers()" type="text" id="CurBal" name="CurBal"  />
           </td>
       </tr>

       <tr>
           <td>Balance: </td>
           <td>
               <input type="text" id="Balance" name="Balance" readonly="readOnly" />
           </td>
       </tr>
 
Share this answer
 
v2
Some mistakes in your code:
1. it should be != not =!
2. redundant
</td>

3. forgot to remove the php code which was supposed to get the current balance from somewhere (database?)
See the modified version below:
XML
<html>
<body>
<table>
<tr>
   <td>  Collections: </td>
    <td>   <input type="text" onchange="addNumbers()" id="Collect" name="Collect" value="0" />
  </tr>
  <tr>
    <td>  Deposits: </td>
    <td>  <input  onchange="addNumbers()" type="text" id="Deposit" name="Deposit" value="0" />
  </tr>
  <tr>
    <td>  Current Balance: </td>
    <td>  <input  type="text" id="CurBal" name= "CurBal" value="100" readonly="readonly" />
  </tr>
  <tr>
    <td>  Balance: </td>
    <td>  <input type="text" id="Balance" name="Balance" value="" />
</tr>
</table>
<script>
function addNumbers() {
  var collect = parseFloat(document.getElementById("Collect").value);
  var dep     = parseFloat(document.getElementById("Deposit").value);
  var curr    = parseFloat(document.getElementById("CurBal").value);
  var bal     = document.getElementById("Balance");

  bal.value = (collect + curr - dep).toFixed(2);
}
</script>
</body>
</html>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900