Skip to content
Advertisement

Total sum of a filtered table using vanilla JS

The only consideration for this question is, at the moment, I’m not using jQuery. Here’s the code:

JavaScript
JavaScript

So, at the moment, whenever I load the page, the <span> tag already has the total amount if we consider all rows. But, whenever I filter the table, the value there doesn’t change. At first, I tried to do it with a button using onclick, but every time I clicked it, it changed from the total value to ‘undefined’. That’s why I removed that button, and I’m considering to update the content in the <span> tag with the onload segment of my code, but I’ve been unable to achieve it so far.

Any help is greatly appreciated, cheers!

Advertisement

Answer

You could replace your getSum function with the following

JavaScript

Basically you first filter the rows based on their display value to get just the visibleRows and then sum up the values.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement