...

View Full Version : Calculate many textfield



Anakdajal
07-22-2011, 03:27 PM
anyone would you help me

I have some problem to calculate

<table width="415" cellspacing="1" cellpadding="1">
<tr>
<th width="151" scope="col">Price</th>
<th width="189" scope="col">qty</th>
<th width="63" scope="col">total</th>
</tr>
<tr>
<td><label>
<input name="price" type="text" id="price" value="2000">
</label></td>
<td><label>
<input name="qty" type="text" id="qty" value="2">
</label></td>
<td><label>
<input name="total" type="text" id="total" value="4000">
</label></td>
</tr>
<tr>
<td><input name="price" type="text" id="price" value="2000"></td>
<td><input name="qty" type="text" id="qty" value="4"></td>
<td><input name="total" type="text" id="total" value="8000"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Total</td>
<td><label>
<input name="gtotal" type="text" id="gtotal" value="12000">
</label></td>
</tr>
</table>
How to calucate it from price * qty = Total and gtotal=sum of total, in the first rows.
Ican do it if there is one row, but i dont know how to calculate samae name of text field like in 2nd or more rows :(

Philip M
07-22-2011, 07:00 PM
You may not have duplicate ids such as price. Give each element a unique id such as price1 etc. Then loop through:



var gtot = 0;
for (var i =1; i<= numberofrows; i++) {
var p = "price" + i;
var q = "qty" + i;
var t = "total" + i;
var prc = document.getElementById(p).value;
var quant = document.getElementById(q).value;
var tot = prc * quant;
document.getElementById(t).value = tot;
gtot += tot;
}
document.getElementById("gtotal").value = gtot;
gtot = 0; // reset grand total



Quizmaster: How many FA Cup Final appearances has Ashley Cole made? Is it three or six?
Contestant: Four.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum