Try this:
<HTML>
<HEAD>
<script>
function this_total( for_what )
{
var this_row = for_what.parentElement.parentElement;
var qty = this_row.getElementsByTagName("td")[1].getElementsByTagName("input")[0].value;
var up = this_row.getElementsByTagName("td")[2].getElementsByTagName("input")[0].value;
var total = qty * up;
for_what.value = total;
return;
}
</script>
</HEAD>
<BODY>
<form name="form1">
<table border="1">
<tr>
<th> S.NO </th>
<th> Qty </th>
<th> Unit price </th>
<th> Total </th>
</tr>
<tr>
<td> <input type="text" name="sno"> </td>
<td> <input type="text" name="qty"> </td>
<td> <input type="text" name="up"> </td>
<td> <input type="text" name="total" onFocus="this_total( this )"> </td>
</tr>
<tr>
<td> <input type="text" name="sno"> </td>
<td> <input type="text" name="qty"> </td>
<td> <input type="text" name="up"> </td>
<td> <input type="text" name="total" onFocus="this_total( this )"> </td>
</tr>
<tr>
<td> <input type="text" name="sno"> </td>
<td> <input type="text" name="qty"> </td>
<td> <input type="text" name="up"> </td>
<td> <input type="text" name="total" onFocus="this_total( this )"> </td>
</tr>
</table>
</form>
</BODY>
</HTML>
It will work assuming that you don't change the positioning relationships within the table. If you can't see what's happening there:
- The onfocus event calls the this_total function passing in a parameter value 'this'. In that context; 'this' refers to the input element that was focused. Inside the this_total function, that parameter is called 'for_what' (can't use 'this' in the function, because it means something different there)
- This line var this_row = for_what.parentElement.parentElement; find the parent of the parent of the input element (that is, the table row)
- This line: var qty = this_row.getElementsByTagName("td")[1].getElementsByTagName("input")[0].value; get reference to the SECOND (array indexes start from 0) td element in the table row; then the first input element in the table row; then the value of that element.
- Rest is self explanatory; there's no need to 'find' the total cell; because it's referenced already as 'for_what'
By the way; I get errors if I rename the function to "total". That indicates that 'total' may be a reserved keyword in JS....