[Solved] jQuery – Passing only certain values into a running total


I presume your full code looks something like:

$('#table1 .set2').each(function (idx) {
    total += parseInt($(this).html(),10);
});

What you’ll need to do is use the mod operator like so:

$('#table1 .set2').each(function (idx) {
    if ((idx + 1) % 5 === 0 && idx !== 19)) {
        total += parseInt($(this).html(),10);
    }
});

Every 5th value apart from 20th. Note the + 1 and the 19 because the it’s zero indexed.

Another way to do this with arbitrary values would be like so:

$('#table1 .set2').each(function (idx) {
    if ([4, 9, 14].indexOf(idx) !== -1) {//5th, 10th and 15th
        total += parseInt($(this).html(),10);
    }
});

Edited to make code easier to read.(Using if rather than ternary operator)

3

solved jQuery – Passing only certain values into a running total