View Full Version : Calculate many textfield

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

I have some problem to calculate

<table width="415" cellspacing="1" cellpadding="1">
<th width="151" scope="col">Price</th>
<th width="189" scope="col">qty</th>
<th width="63" scope="col">total</th>
<input name="price" type="text" id="price" value="2000">
<input name="qty" type="text" id="qty" value="2">
<input name="total" type="text" id="total" value="4000">
<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>
<input name="gtotal" type="text" id="gtotal" value="12000">
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, 08: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.