I am adding values to table like: Item,Quantity,Price,TotalPrice
Now there are multiple rows: How can i sum TotalPrice of all to get GrandTotal using Jquery.
Code:
$("#Product").append(" <tr><td id='clientname'>" +ClientName+ "</td> <td id='item'>"+ItemName+"</td> <td id='quantity'>"+Quantity+"</td> <td id='price'>"+Price+"</td> <td id='totalprice'>"+TotalPrice+"</td> <td> <a onClick='deleteRow(this);'>Delete</a> </td> </tr>");
Its possible when i insert new row data its show grand total in textbox/label,Like:
function TotalPriceCalc() { var lblTotalPrice = document.getElementById('lblTotalPrice'); lblTotalPrice.value = sum; }
Advertisement
Answer
After you use class=
instead of id=
.Cause ID MUST be unique. you need to loop through each row and find totalPrice
$(document).ready(function(){ var TotalValue = 0; $("#Product tr").each(function(){ TotalValue += parseFloat($(this).find('.totalprice').text()); }); alert(TotalValue); });
While you tagged Jquery .. This is a Jquery solution so please be sure to include Jquery