html
<tr class="total" data-par="A,B"> <!-- for A-B -->
<tr class="total" data-par="C,D"> <!-- for C-D -->
js
total_par.each(function () {
totals = [0, 0, 0];
var par = $(this).data('par').split(','); // par[0] = A and par[1] = B
$('[data-parent="' + par[0] + '"]').each(function () { //for A
totals[$(this).parent('td').index() / 2 - 1] += +this.value; // add all values
});
$('[data-parent="' + par[1] + '"]').each(function () {//for B
totals[$(this).parent('td').index() / 2 - 1] += -1 * +this.value;// subtract all values --> number is multiplied with -1 to make it negative and than added
});
$(this).find('input').val(function (i) {
return totals[i]; //assign values to current input element inside class total
});
});
or
total_par.each(function () {
totals = [0, 0, 0];
var par = $(this).data('par').split(',');
$('[data-parent="' + par[0] + '"]').each(function () {
totals[$(this).parent('td').index() / 2 - 1] += +this.value;
});
$('[data-parent="' + par[1] + '"]').each(function () {
totals[$(this).parent('td').index() / 2 - 1] -= +this.value;// subtract all values
});
$(this).find('input').val(function (i) {
return totals[i];
});
});
fiddle Demo
find_parent_row.nextUntil('[class*="parent-"]').find('input[data-parent="' + parent_name + '"]').each(function () {
totals[$(this).parent('td').index() / 2 - 1] += +this.value;
});
added .find('input[data-parent="' + parent_name + '"]')