0

Ok what I have is a loan enquiry form and when the visitor inputs data into 2 text fields it add the two together and pre-populates another text field

<script type="text/javascript">
  $(function () {
        var amtreq = $('input:text[id$=amtreq]').keyup(bar);


        function bar() {
            var loan = amtreq.val();
            var percent = (loan / 100 * 2)
            sum2 = add2(loan + percent);
            $('input:text[id$=totalloan]').val(sum2);
        }

        function add2() {
            var sum2 = 0;
            for (var i = 0, j = arguments.length; i < j; i++) {
                if (IsNumeric2(arguments[i])) {
                    sum2 += parseFloat(arguments[i]);
                }
            }
            return sum2;
        }
        function IsNumeric2(input) {
            return (input - 0) == input && input.length > 0;
        }
    });

However what I am trying to do is to calculate a percentage of the total number

I would like like to calculate 2% of the data in totalsecurity and then to add this value to the value of totalsecurity and then display the result in the textfield with id "totalloan"

** EDIT : noticed I posted wrong bit of code ))

It is calculating the percentage ok now however it doesnt add the 2 together it is simply appending the percent value onto the end of loan value

  • use `parseInt(textBox1.val());` and same for other. Or use `paresFloat(textBox1.val())` – Alive to die - Anant May 23 '18 at 11:09
  • 1. Why are you using .val() on totalsecurity, then .val on the line after? Surely it should be val() for the percent, then parseInt() on this? 2. Your percent figure will be wrong regardless (BODMAS), the 100 * 2 will be done first in the calculation. – Chris Dixon May 23 '18 at 11:11
  • You need to convert `amtreq.val()` to float. Do: `var loan = +amtreq.val();` – trincot May 23 '18 at 11:23
  • Apologies, just realised I posted wrong selection of code , have also amended question to reflect new code – Love Crypto May 23 '18 at 11:24
  • @trincot THANK YOU ! That was bugging me for days , I'm completely new to jquery & javascript ( I'm a PHP guy ) many thanks for your help – Love Crypto May 23 '18 at 11:26
  • Possible duplicate of [how to sum two numbers from input tag?](https://stackoverflow.com/questions/11961474/how-to-sum-two-numbers-from-input-tag) – trincot May 23 '18 at 11:26

0 Answers0