Click to See Complete Forum and Search --> : For loop


Breeze
12-28-2003, 01:53 PM
I have a simple script to grab the quantity of an item ordered, multiply it by the price, and then display the total.

var qty1 = document.form.Qty1.value;
var price1 = document.form.Price1.value;
cost1 = (qty1 * price1);
document.form.total1.value=cost1;


var qty2 = document.form.Qty2.value;
var price2 = document.form.Price2.value;
cost2 = (qty2 * price2);
document.form.total2.value=cost2;


But I have lots of items so want to do some kind of loop rather than writing these lines over and over. Thanks.

Khalid Ali
12-28-2003, 02:06 PM
typical ths is what I do in a situation like this

Since price is not allowed to be changed,and the only thing that a user might add is add quantity(by default typically 1)

I'd add an onkeyup event to the quantity field and then call a function that will calculate the net toal for that particular row.

onkeyup="Calculate(this.value,'20.23')";

now in calculate function may look like this
function Calculate(qty,price){
var net_t = Number(qty) * Number(price);
}