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

Question

Question

I have a number auto-populating from a Lookup. How do I make that number negative?

asked on December 16, 2015

I have a number auto-populating from a lookup. How would I go about making that number negative?

 

 

I need this field to automatically change the number to a negative.

 

Thanks,

0 0

Answer

SELECTED ANSWER
replied on December 18, 2015

Try this, should work better:

$(document).ready(function () {
    $(document).on('change', '.sum6_2 input', function() {
      $(this).val(-this.value);
    }).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;
    }
});

 

1 0

Replies

replied on December 16, 2015
$(document).ready(function() {
     $(document).on('change', '.sum6 input', function() {
          $(this).val(-this.value);
     });
});

The above code should do the trick.

1 0
replied on December 16, 2015

Chris,

So that kind of works.. Here's the issue. I have it adding the two fields on the left, and showing the total on the right:

My current code is:

$(document).ready(function () {
    $('.cf-table-block').on('change', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('change', 'input', rowtotal);
    }
    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);
    }
    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;
    }
});

which is working great. I tried to put your code inside this:

$(document).ready(function () {
    $('.cf-table-block').on('change', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('change', 'input', rowtotal);
    }
  $(document).ready(function() {
     $(document).on('change', '.sum6 input', function() {
          $(this).val(-this.value);
     });
});
    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);
    }
    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;
    }
});

But it makes it do this:

It turns both of the first two fields negative (I imagine because they have the same CSS class). Then it seems to take the first value and adds the positive value from the second field. Or maybe it is subtracting a negative? Either way it is showing -1, when I need it to show 9 -8 = 1.

Any ideas?

0 0
replied on December 16, 2015

My first suggestion is to add another class to the number you want to be automatically changed to negative. Once you have this second class added then change the code to:

$(document).ready(function () {
    $('.cf-table-block').on('change', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('change', 'input', rowtotal);
    }
     $(document).on('change', '.[secondaryclassname] input', function() {
          $(this).val(-this.value);
     });
    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);
    }
    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;
    }
});

That should get it working correctly. 

0 0
replied on December 17, 2015

I tried your suggestion - here is the result:

 

Here's the code:

$(document).ready(function () {
    $('.cf-table-block').on('change', 'input', sumtotal);
    if ($('.subtotal').length > 0) {
        $('.cf-table-block').on('change', 'input', rowtotal);
    }
     $(document).on('change', '.sum6_2 input', function() {
          $(this).val(-this.value);
     });
    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);
    }
    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;
    }
});

Any suggestions?

0 0
replied on December 17, 2015

Did you "add" the class or just "rename" the class from "sum6" to "sum6_2". In the classname, it should look like "sum6 sum6_2".

1 0
replied on December 17, 2015

I literally just realized you could add classes. I did that and now this is the result: 

0 0
replied on December 17, 2015

Looks like you need to refactor your code. You have too many events firing on the inputs at one time. This will cause unexpected results. I will refactor it and post it here once done.

1 0
replied on December 18, 2015 Show version history

Any ideas on this Chris? 

It looks like the calculation is being performed, and then the number is being changed to negative. So how would I go about changing the sign of the number, and then performing the calculation?

0 0
SELECTED ANSWER
replied on December 18, 2015

Try this, should work better:

$(document).ready(function () {
    $(document).on('change', '.sum6_2 input', function() {
      $(this).val(-this.value);
    }).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;
    }
});

 

1 0
replied on December 18, 2015 Show version history

You're the champ! Thank you!

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

Sign in to reply to this post.