I have the following jQuery code to find the sum of all the rows of a table, now it finds the sum on the keyboard. I want the amount already calculated. I wanted to have a hidden field as an input with a value, so the amount is automatically calculated.
Now there is an input, the user writes a certain number, and how he gets the amount, I try to get the amount automatically. it works, but I need print output in the text box .... for this code
<table>
<tr>
<td width="40px">1</td>
<td>Number</td>
<td><input class="txt" type="text" name="txt" /></td>
</tr>
<tr>
<td>2</td>
<td>Number</td>
<td><input class="txt" type="text" name="txt" /></td>
</tr>
<tr>
<td>3</td>
<td>Number</td>
<td><input class="txt" type="text" name="txt" /></td>
</tr>
<tr id="summation">
<td colspan ="2" align="right">
Sum :
</td>
<td>
<span id="sum"><input class="txt" type="text" name="txt" />
</td>
</span>
</td>
</tr>
</table>
$(document).ready(function() {
calculateSum();
$(".txt").live("keydown keyup", function() {
calculateSum();
});
});
function calculateSum() {
var sum = 0;
$(".txt").each(function() {
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
$(this).css("background-color", "#FEFFB0");
}
else if (this.value.length != 0){
$(this).css("background-color", "red");
}
});
$("#sum").html(sum.toFixed(2));
}
source
share