You are viewing limited content. For full access, please sign in.

Question

Question

How to a change the order in which functions are executing?

asked on December 21, 2015 Show version history

I have a number being copied from one field to another. I then have a calculation being done using the number that was just copied over. For some reason, though, the calculation is running before the number is copied over. Here is my code: 

$(document).ready(function () {
          $(document).ready(function () {
            $('.numberinput input').on('change', function() {
            $('[name="Field84(1)"]').val($('.numberinput input').val());
        });
    }).on('change','.cf-table-block input', sumtotal);
    function sumtotal() {
        var sum = 0;
        $('td.sum6').each(function () {
            var s = 0;
            $(this).find('input').each(function () {
                s += parseNumber($(this).val());
            });
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
        $('.total6 input').val(sum);
          if ($('.subtotal').length > 0) rowtotal();
    }
    function rowtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0;
            $(this).find('.sum6 input').each(function () {
                s += parseNumber($(this).val());
            })
            $(this).find('.subtotal input').val(s);
            sum += s;
        });
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

Here is a screen shot:

So I need -1 from "Single Line" to copy into "Field84(1)" which is the editable field. Right now the calculation is doing 3 + 0 = 3. And then it is copying -1 in... so it's not part of the calculation. How do I make the calculation wait until the number has been copied in?

0 0

Replies

replied on December 21, 2015

Hi David,

I would take out "$(document).ready(...).on(...)" altogether and replace it with:

  $('.numberinput input, .cf-table-block').change(function() {
    
    $('[name="Field81(1)"]').val($('.numberinput input').val());
    sumtotal();
  });

This way, you know that your "Adjustment" field will be filled before the sumtotal() function runs.

Let me know if this works for you!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.