Skip to content
Advertisement

How do I sum the price column from HTML table using JavaScript?

I am trying to add Price from table column to a total. I am having problem adding values such as 10.00 or 5.99. I am able to calculate prices with int values, but not with values 10.00 or 5.99, etc.

Here is what I have below.

JavaScript
JavaScript

Advertisement

Answer

You have three issues:

  1. You are grabbing the wrong cell index, indices start at 0:
    • table.rows[i].cells[1]
  2. You need to call the correct parse function:
    • parseFloat(table.rows[i].cells[1].innerHTML);
  3. You need to format your output:
    • "SubTotal = $" + sumVal.toFixed(2);

Update: Added functionality for removing rows.

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