Identify Row and Column of Cell in a Table

by Joe Saad

HTML

<table border="1" bordercolor="#FFCC00" width="400">
    <tr>
        <th>Table Col 1</th>
        <th>Table Col 2</th>
         <th>Table Col 3</th>
        <th>Table Col 4</th>
    </tr>
    <tr>
        <td>00</td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
    </tr>
    <tr>
        <td>10</td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
    </tr>
    <tr>
        <td>20</td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
        <td><input type="text" /></td>
    </tr>
    <tr class="totals">
        <td>Totals</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
</table>

JavaScript

$(document).ready(function(){
  $("td").click(function(){
     var col = $(this).parent().children().index($(this));
    var modCol = col+1;
   var total   = 0;
  var row = $(this).parent().parent().children().index($(this).parent());
      alert("row:"+ row + " col:"+modCol);
      alert($("td:nth-child(4) input").val());
    $("td:nth-child("+modCol+") input").each(function(){
       
       total = parseFloat(total) + parseFloat($(this).val());

    });
   
    $(".totals td:nth-child("+modCol+")").text(total);
  });
});