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,
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,
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; } });
$(document).ready(function() { $(document).on('change', '.sum6 input', function() { $(this).val(-this.value); }); });
The above code should do the trick.
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?
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.
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?
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".
I literally just realized you could add classes. I did that and now this is the result:
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.
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?
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; } });