1

This link shows how I tried to add two text box values but the addition of the value is wrongly calculated.

This my javascript code:

$("#amount").on("input", function () 
{
    const originalDebit = $('#debit').val();
    const amount = $('#amount').val();
    const tot = parseFloat(originalDebit) + parseFloat(amount);
    $('#debit').val(tot);
});
23
  • hi tom its giving correct value, you enter 1 and the out put shall be 12346.. Commented Jun 29, 2019 at 6:06
  • use three digit number Commented Jun 29, 2019 at 6:06
  • you got it man, check the solution given below it was happening because amount was becoming NaN. Commented Jun 29, 2019 at 6:13
  • Hi Tom! Still at it I see. Commented Jun 29, 2019 at 7:07
  • @ChrisG i posted it again Commented Jun 29, 2019 at 7:09

2 Answers 2

1

You can check whether the value is a number or not with isNaN() OR empty before doing the addition. In such cases assign 0 to the variable.

Please Note: You also have to declare originalDebit outside of the event handler function to retain the original value on each change:

Try the following way:

const originalDebit = $('#debit').val().trim();
$("#amount").on("input", function () {
  const amountVal = $('#amount').val().trim();
  const amount = (isNaN(amountVal) || amountVal == "") ? 0 : amountVal;
  const tot = parseFloat(originalDebit) + parseFloat(amount);
  $('#debit').val(tot);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="amount">
<p>D<input  value ="12345" id="debit"></p>

Sign up to request clarification or add additional context in comments.

13 Comments

the value is not refreshing.if you clear the answer the old value is not displaying
@Tom, declare originalDebit outside the event handler function, updated the answer please check:)
you write but when i write you code in my coding page the result is not coming for me.
yes working i didn't say no.In js fiddle it is working
@Tom, then, I guess the issue could be somewhere else in your code in your actual environment!!!
|
1

I hope this help you. I used javascript vanilla.

document.addEventListener('DOMContentLoaded', () => {

  document.getElementById('amount').addEventListener('input', (e) => {

    let {
      value
    } = e.target;
    let debit = document.getElementById('debit').value;

    value = value == '' ? 0 : parseFloat(value);
    debit = debit == '' ? 0 : parseFloat(debit);

    document.getElementById('debit').value = value + debit

  })

})
<input type="number" id="amount">
<input type="number" id="debit">

4 Comments

This code has the same error as OP's (it keeps adding to debit with every keystroke).
@ChrisG i posted same question now. i used the variable outside handler but for my environment the value is not take for calculation
in that case there is a problem of logic. what he try to do is not the right way
he could use the event focusout or a button to trigger the event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.