0

I have the below

$('.amount2').on("input", function() {
  var total = 0;
  var i = 0;
  $('#manifest-table25 .amount2').each(function() {

    total += $('#id_form-'+(i)+'-Amount').val();
    i++;
  })
  
  $('#id_InvoiceTotal').val(total);
});

What is confusing me is that when I enter the value 5 into the input I would expect for the total to equal 5, instead it shows on the UI as 05. How do I remove the leading zero/any thoughts on what is causing it? It is an integerfield in the model not charfield.

1
  • Please make / add minimal reproducible example it is helping us help you, its faster for everyone if we don't have to write it our-self to test things out. Use <> button in editor to make a snippet. You will increase a chance for fast help by a lot... Commented Aug 10, 2020 at 22:31

1 Answer 1

2

$.val() return a string.
You need to parse it into an integer or a float before doing some math:

$('.amount2').on("input", function() {
  var total = 0;
  var i = 0;
  $('#manifest-table25 .amount2').each(function() {
    total += parseInt($('#id_form-'+(i)+'-Amount').val());
    i++;
  })
  
  $('#id_InvoiceTotal').val(total);
});
Sign up to request clarification or add additional context in comments.

Comments

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.