9grid Jquery

How to calculate column value in Jquery?

We need to calculate column value when we are working with items that having respective prices for each row. So at bottom we can calculate total price.

HTML :

<b>Calculate Total Item Price</b>

<table border=”1″>

    <tr><td>Items Name</td><td>Price</td></tr>

    <tr><td>Item1</td><td>49</td></tr>

    <tr><td>Item2</td><td>1211</td></tr>

    <tr><td>Item3</td><td>1</td></tr>

    <tr><td>Item4</td><td>12</td></tr>

</table>

JS:

$(document).ready(function(){

var result = [];

  $(‘table tr’).each(function(){

   $(‘td’, this).each(function(index, val){

     if(!result[index]) result[index] = 0;

      result[index] += parseInt($(val).text());

    });

  });

  $(‘table’).append(‘<tr></tr>’);

  $(result).each(function(){

   $(‘table tr’).last().append(‘<td>’+this+'</td>’)

  });

});

O/P:


Leave a comment

Your email address will not be published. Required fields are marked *