Sum Values from Column in HTML

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

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.

updateSubTotal(); // Initial call
function updateSubTotal() { var table = document.getElementById("myTable"); let subTotal = Array.from(table.rows).slice(1).reduce((total, row) => { return total + parseFloat(row.cells[1].innerHTML); }, 0); document.getElementById("val").innerHTML = "SubTotal = $" + subTotal.toFixed(2);}
function onClickRemove(deleteButton) { let row = deleteButton.parentElement.parentElement; row.parentNode.removeChild(row); updateSubTotal(); // Call after delete}
#myTable td {  padding: 0.25em;}
#val { display: block; margin-top: 0.5em;}
<table id="myTable">  <tr>    <th>Item</th>    <th>Price</th>    <th>Remove</th>  </tr>  <tr>    <td>Hoodie</td>    <td class="count-me">15.00</td>    <td><button onClick="onClickRemove(this)">Remove</button></td>  </tr>  <tr>    <td>Nike Cap</td>    <td class="count-me">10.99</td>    <td><button onClick="onClickRemove(this)">Remove</button></td>  </tr></table><span id="val"></span>

How to sum values in an HTML table column

The problem with your code is that you are looping through the class elements, and still using the initial selector for computing sum. This means that inner selector will select all inputs that are descendant of the 'itemPriceClass'. To overcome this, change

mySum += parseFloat($('.itemPriceClass input').val());

to

mySum += parseFloat($(this).find('input').val());

so that only input that is descendant of current row that is being looped through is added to the sum.

sum all values for table column on html page

There were a few errors in your code. Here is the working version : http://jsfiddle.net/2q06sv8w/3/

$(document).ready(function () {
var expenses = $('.expenses');
var expenseTotal = $('#expenses_sum');
expenseTotal.html('0');
$.each(expenses, function (index, object) {
var boldTag = $(object).find('b');
if (boldTag && boldTag.length > 0 && $(boldTag[0]).html() != '') {
expenseTotal.html(parseInt(expenseTotal.html()) + parseInt($(boldTag[0]).html().replace(/,/g, '')));
}
})
});
  1. The selector '.' is used to select elements matching a class name and '#' is used to select an element matching an id. So 'expenses_sum' must be an id and not a class.
  2. You should use html() and not val() to get and set the content of a td tag.
  3. The td tag contains the value inside a bold tag so you must use the html() on the bold tag.
  4. Declaring global variables and using $ prefix for variables does not look necessary here.

Sum values from column in html

first, you need to select each 3rd column, which can be done using #second td:nth-child(3). second, you need to trim the Cena: from each value using .replace('Cena: ',''), and then using parseInt() to caste value to an integer. third, sum the values and display the result.

var sum = 0;
$("#second td:nth-child(3)").each(function(){
sum += parseInt($(this).text().replace('Cena: ',''));
});
$("#yourDesiredElement").html(sum);

this would be placed inside your $("#first tr").filter(".selected").each(function( index ){ but after your $("#second").append(.

Sum column values in table

I guess what you want to do is something like this:

DEMO: https://jsfiddle.net/zxooa1j4/1/

var sum1 = 0;
var sum2 = 0;
$("#category tr").not(':first').not(':last').each(function() {
sum1 += getnum($(this).find("td:eq(2)").text());
sum2 += getnum($(this).find("td:eq(3)").text());
function getnum(t){
if(isNumeric(t)){
return parseInt(t,10);
}
return 0;
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
});
$("#sum1").text(sum1);
$("#sum2").text(sum2);

isNumeric() function is quoted from this answer:

Is there any function like IsNumeric in javascript to validate numbers

HTML:

<table border=1 id="category">
<tr>
<th>User Name</th>
<th>User Belongs</th>
<th>Total Work Assigned</th>
<th>Not Called</th>
</tr>
<tr>
<td>vidyaranyapura</td>
<td>Category</td>
<td>172</td>
<td>156</td>
</tr>
<tr>
<td>sahasra</td>
<td>Company</td>
<td>500</td>
<td>350</td>
</tr>
<tr>
<td>global</td>
<td>Not Assigned</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td></td>
<td></td>
<td id="sum1"></td>
<td id="sum2"></td>
</tr>
</table>

Hope this helps.

How to get value from input in table column and sum them up?

Try this code in your javascript:

$(document).ready(function () {
$('input[name="total_amount"]').val(calcSum());
$('input[name="amount"]').change(function () {
$('input[name="total_amount"]').val(calcSum());
});
});

function calcSum() {
var tamount = 0;
$('input[name="amount"]').each(function() {
tamount += parseInt(($(this).val() ? $(this).val() : 0));
});
return tamount;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="data">
<tr>
<th>No</th>
<th>Description</th>
<th>Amount</th>
</tr>
<tr>
<td>1</td>
<td><input type="text" name="descrpt"></td>
<td><input type="number" name="amount"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text" name="descrpt"></td>
<td><input type="number" name="amount"></td>
</tr>
<tr>
<td>3</td>
<td><input type="text" name="descrpt"></td>
<td><input type="number" name="amount"></td>
</tr>
</table>

<div>
<h5>Total Amount :<input type="number" name="total_amount" readonly></h5>
</div>

Trying to sum the values of a dynamic HTML table column

You will want to use tr > td:nth-child(2) selector to select elements in the second column of a table row. Code edited to be as simple as possible - it's simpler to use Number(x) || 0 for the same logic as your two functions.

var sum1 = 0;
$("#output_table tr > td:nth-child(2)").each( (_,el) => sum1 += Number($(el).text()) || 0);
$("#sum1").text(sum1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table id="output_table">  <tr>    <th>Name</th>    <th>Value</th>  </tr>  <tr>    <td>Bananas</td>    <td>10</td>  </tr>  <tr>    <td>Apples</td>    <td>5</td>  </tr>  <tfoot>    <tr>      <th id="total" colspan="1">Total CPC:</th>      <td id="sum1"></td>    </tr>  </tfoot></table>

In HTML or JavaScript, display sum of columns and rows in a table in the table

My table:

 <table border="1">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input class="tdvalcol1" value = "0"></input>
</td>
<td>
<input class="tdvalcol2" value = "0"></input>
</td>
<td class="totalrow">
Total:
</td>
</tr>
<tr>
<td><input class="tdvalcol1" value = "0"></input></td>
<td><input class="tdvalcol2" value = "0"></input></td>
<td class="totalrow">
Total:
</td>
</tr>
<tr>
<td id="totalcol1">
Total:
</td>
<td id="totalcol2">
Total:
</td>
<td id="totalcorner">
Total:
</td>
</tr>
</tbody>
</table>

the jquery:

 col1Sum =0; 
col2Sum =0;
rowtotals =0;

$(".tdvalcol1").each(function(index,value) {
tdvalue = this.value;
col1Sum += parseFloat(tdvalue);
});
$("#totalcol1").text(col1Sum);

$(".tdvalcol2").each(function(index,value) {
tdvalue = this.value;
col2Sum += parseFloat(tdvalue);
});
$("#totalcol2").text(col2Sum);

$("tr").each(function() {
rowSum =0;
var cols = $(this).find("input");
cols.each(function() {
rowSum += parseFloat(this.value);
});
rowtotals +=rowSum;
$(this).find(".totalrow").text(rowSum)
});

Total sum of html table column values(dynamic)

first of all, I added "int-input" class to that had value in it
and afther that I selected all of them

var totalValues = document.querySelectorAll(".int-input");
var total = 0;
totalValues.forEach((k) => {
total += parseInt(k.innerHTML);
});

and then I add number to result

function updatevalue(item, value, data) {
var td = item.parentNode.nextElementSibling;
var onupdate = 0;
if (value) {
onupdate = data * value;
} else if (value === 0) {
onupdate = data * 0;
} else {
onupdate = data * 0;
}
td.innerHTML = onupdate;
var totalValues = document.querySelectorAll(".int-input");
var total = 0;
totalValues.forEach((k) => {
total += parseInt(k.innerHTML);
});
document.querySelector(".total").innerHTML = total;
}

function add_item(item, next) {
item.parentNode.style.display = "none";
next.style.display = "block";
item.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.value = '';
item.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.disabled = false;
}

function remove_item(item, prev) {
item.parentNode.nextSibling.nextSibling.firstChild.disabled = true;
item.parentNode.nextSibling.nextSibling.firstChild.value = '0';
item.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.innerHTML = '0';
item.parentNode.style.display = "none";
prev.style.display = "block";
}
<table>
<th>Select</th>
<th>Intake</th>
<th>Total</th>
{% for a in top_energy %}
<tr>
<td><input id="btn" type="button" value="Add" name="{{a.Food}}" onclick="add_item(this, this.parentNode.nextSibling.nextSibling)"></td>
<td style="display: none;text-align: center;"><input id="btn" type="button" name="{{a.Food}}" value="Remove" onclick="remove_item(this,this.parentNode.previousElementSibling)"></td>
<td><input type="Number" name="some" id="{{a.Food}}" value="0" oninput="updatevalue(this,this.value,9)" disabled></td>
<td class="int-input">0</td>
</tr>

<tr>
<td><input id="btn" type="button" value="Add" name="{{a.Food}}" onclick="add_item(this, this.parentNode.nextSibling.nextSibling)"></td>
<td style="display: none;text-align: center;"><input id="btn" type="button" name="{{a.Food}}" value="Remove" onclick="remove_item(this,this.parentNode.previousElementSibling)"></td>
<td><input type="Number" name="some" id="{{a.Food}}" value="0" oninput="updatevalue(this,this.value,9)" disabled></td>
<td class="int-input" onkeyup="totalval(this)">0</td>
</tr>

<tr>
<td><input id="btn" type="button" value="Add" name="{{a.Food}}" onclick="add_item(this, this.parentNode.nextSibling.nextSibling)"></td>
<td style="display: none;text-align: center;"><input id="btn" type="button" name="{{a.Food}}" value="Remove" onclick="remove_item(this,this.parentNode.previousElementSibling)"></td>
<td><input type="Number" name="some" id="{{a.Food}}" value="0" oninput="updatevalue(this,this.value,9)" disabled></td>
<td class="int-input" onkeyup="totalval(this)">0</td>
</tr>
<tr>
<td>I want the sum of total column whenever the value of any cell in total column gets changed</td>
<td class="total">0</td>
<td>sum</td>
</tr>
{% endfor %}

</table>


Related Topics



Leave a reply



Submit