Hi, i have actually 2 problems.
1) I need to calculate a total dynamically.
I have an input with the price of a product and next the amount. As the users inserts the amount I would like the total box to give the amount*price number. So if the price is 3 and user inserts:
1 - then total should display 3. Then the user inserts 0 (amount is now 10) total should display 30.
The problem is that when i ask for the value in the onkeyup event of the amount input i don't get the value with the last key (same with onkeypress event). I could add that number (if it's a number) or see if it's backspace or any char but there should be an easy answer.
I solved it by setting a timeout and using a callback function so as to have the latest value of the amount but it's an awfull solution (though not expensive in code).


2) Avoid focus reset when i update the value of the amount.
I've managed to add commas on the fly to the amount like when the user types "1000" the number displayed is "1,000". In firefox it works great but in IE and Chrome it resets the focus each time i update the field. So when you try to move your cursor back it returns to the last position (maybe because the value of the input is changed).

here's my code:

Code:
function updatePrice(){
	setTimeout(function(){callback()}, 10);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function callback()
{
	var price = document.getElementById('price').getValue();
	var amount = document.getElementById('amount').getValue().replace(/\,/g,'');
	var result = (amount*price);
	var cash = document.getElementById('cash').getValue().replace(/\,/g,'');
	
	document.getElementById('amount').setValue(addCommas(amount));
	document.getElementById('total').setValue(addCommas(result.toFixed(2)));
	
	if(result > cash)
		document.getElementById('total').setStyle('color', 'red');
	else
		document.getElementById('total').setStyle('color', 'green');
}
and html/php:

Code:
<input id="cash" type="text" size="20" maxlength="45" name="cash" value="<?php echo number_format(abs($cash),2); ?>
<input id="price" type="text" size="20" maxlength="45" name="price" onkeyup="updatePrice();">
<input id="amount" type="text" size="20" maxlength="45" name="amount" onkeyup="updatePrice();"/>
<input id="total" type="text" size="20" maxlength="45" name="total" readonly />
any help??
thanks!