Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Why NaN?
I want to show difference between 'Total' and 'Paid' amount is 'Total Due' in JavaScript. How to show?

When I calculate 'Subtotal' and 'Tax' the ans will show in 'Total' cell and, When I'm trying to show 'Total Due' from (Total-Paid) then print NaN in 'Total Due' cell in JavaScript
How to do?

What I have tried:

JavaScript :

function taxcal()
{
  var per=100;
  var subtotal=parseInt(document.getElementById('subtotal').value);
  var tax=parseInt(document.getElementById('tax').value);
  var total=parseInt(document.getElementById('total').value);
 
  if (isNaN(tax)) tax=0;

  var total1=(subtotal*(tax/per).toFixed(2));
  total=total1+subtotal;
  document.getElementById("total").innerHTML=total;
  console.log(document.getElementById('total').value);
}

function totaldue()
{
  var total=parseInt(document.getElementById('total').value);
  var paid=parseInt(document.getElementById('paid').value);
  var totaldue=parseInt(document.getElementById('totaldue').value);

  var totaldue1=Math.abs((total-paid).toFixed(2));
  document.getElementById("totaldue").innerHTML=totaldue1;
}

HTML :
<table>
.
.
.
<td valign="middle" align="center" width="100%">
        <input type="text" id="subtotal" name="subtotal" onKeyUp="result(this.form)" onKeyUp="taxcal(this.form)" class="subtotal" value="0.00" style="border:none;width: 100%;"/>
        <input type="text" id="tax" class="tax" name="tax" onKeyUp="taxcal(this.form)" onKeyUp="totaldue(this.form)" style="border:none;width: 100%;">
    </td>

    </tr>
  </table>
</div>
<br>
<div class="form-group text-center">
  <table style="float: left;margin-left: 25px;" cellpadding="1" cellspacing="1" border="1">
  <tr><td width="70%" bgcolor="#e2dad8"><label style="font-style: bold;"><font size="4px">Comment :</label></td></tr>
  <tr>
    <td><textarea rows="2" style="width: 100%;border:none;" class="form-control"></textarea></td></tr>
  </table>
 
  <table style="float: right;margin-right: 31px;" cellpadding="1" cellspacing="1" border="1" width="30%">
      <tr>
        <td width="20%" bgcolor="#e2dad8"><font size="4px">
          Total
        </td>
        <td width="10%">
          <p name="total" id="total" onKeyUp="totaldue(this.form)" readonly="readonly" value="00.00" class="text-center" style="border: none;"></p>
        </td>
      </tr>
      <tr>
        <td width="20%" bgcolor="#e2dad8"><font size="4px">Paid</td>
        <td width="10%">
          <input type="text" name="paid" onKeyUp="totaldue(this.form)" id="paid" class="text-center" style="border:none;">
        </td>
      </tr>
      <tr>
        <td width="20%"  bgcolor="#e2dad8"><font size="4px">Total Due</td>
        <td width="10%">
          <p class="text-center" id="totaldue" name="totaldue" value="0.00" onKeyUp="totaldue(this.form)" readonly="readonly" style="border:none;"></p>
        </td>
      </tr>
    </table>
</div> 
Posted
Updated 28-Mar-18 2:06am

1 solution

Hi, Here is your solution.
I analysed your code and i found a bug in your code. you just create a mistake in your "totalDue" function. and here the function code
JavaScript
function totaldue()
{
  var total=parseInt(document.getElementById('total').innerHTML);
  var paid=parseInt(document.getElementById('paid').value);
  if(isNaN(paid))
      paid=0;
  var totaldue=parseInt(document.getElementById('totaldue').innerHTML);
  
  var totaldue1=Math.abs((total-paid).toFixed(2));
  document.getElementById("totaldue").innerHTML=totaldue1;
}


And another thing i found in your code, there is no "result" function but you used in following line, which you've to remove.
HTML
<input type="text" id="subtotal" name="subtotal" onKeyUp="result(this.form)" onKeyUp="taxcal(this.form)" class="subtotal" value="0.00" style="border:none;width: 100%;"/>


and that line should be like following
HTML
<pre><input type="text" id="subtotal" name="subtotal"  onKeyUp="taxcal(this.form)" class="subtotal" value="0.00" style="border:none;width: 100%;"/>
 
Share this answer
 

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