Hi all,
I have a simple table set up in which a user can insert values into the boxes and onkeydown it will sum up the column.
Table:
Javascript:
I think my problem is I am getting the wrong objects when traversing through the DOM of the table, think I may be getting the tbody rather than the column, so I need help in getting the columns to sum them up if anyone can help.
Many thanks in advance.
I have a simple table set up in which a user can insert values into the boxes and onkeydown it will sum up the column.
Table:
Code:
<table> <tr> <td><input type="text" onkeydown="calcTotal(this, 'tot')" /></td> <td><input type="text" onkeydown="calcTotal(this, 'tot1')" /></td> </tr> <tr> <td><input type="text" onkeydown="calcTotalCol(this, 'tot')" /></td> <td><input type="text" onkeydown="calcTotalCol(this, 'tot1')" /></td> </tr> <tr> <td><input type="text" onkeydown="calcTotal(this, 'tot')" /></td> <td><input type="text" onkeydown="calcTotal(this, 'tot1')" /></td> </tr> <tr> <td><input type="text" id="tot" /></td> <td><input id="tot1" type="text" /></td> </tr> </table>
Code:
function calcTotal(txtBox, totBox)
{
var col = txtBox.parentNode.parentNode.parentNode;
var inputs = col.getElementsByTagName("input"); //Get input boxes
sum = 0; //Set sum to 0
for (i=0; i< inputs.length; i++)
{
if (inputs[i].type == "text")
{
if(inputs[i].id.indexOf(totBox) == -1) //Pass id of total text box
{
if (inputs[i].value != null && inputs[i].value != "")
sum += parseInt(inputs[i].value); //Calculate the total
}
else
{
inputs[i].value = sum; //Store total in box
}
}
}
}
Many thanks in advance.
Comment