...

View Full Version : textbox enter dollar value, update another with paypal charge included



idunnojs
02-26-2012, 12:29 AM
Looking for an example of how to take the value typed in one textbox to be calculated and update the value of a second texbox on the fly.

The calculation is,

(2.9% X first textbox value) + $0.30 = second textbox value

devnull69
02-26-2012, 12:49 AM
I take it that the value of the first textbox actually is a number (so no characters except digits and a maximum of one dot)?

If that is the case


var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);

idunnojs
02-26-2012, 01:18 AM
Correct

idunnojs
02-26-2012, 01:32 AM
I take it that the value of the first textbox actually is a number (so no characters except digits and a maximum of one dot)?

If that is the case


var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="application/javascript">
var firstvalue = parseFloat(document.getElementById('idOfFirstTextbox').value);
document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);</script>
</head>

<body>
<form>
<input id="idOfFirstTextbox" name="idOfFirstTextbox" type="text" value="" />
<input id="idOfSecondTextbox" name="idOfSecondTextbox" type="text" value="" />
</form>
</body>
</html>

This doesn't appear to work or did I misunderstand implementing this?

devnull69
02-26-2012, 08:04 AM
Yes you did

Before implementing code you should ask yourself what exactly(!) you want to have. Observe your code top-down and you will find out that your code says:

Immediately (before even processing the HTML below) get the value of the first textbox (which doesn't exist yet) and calculate a result into the second textbox. Do this only once, whatever happens.

What you wanted to do is something similar to:

After those two textboxes have been loaded, listen for every change of the first textbox. On every change take the (numerical) input of this textbox and calculate a result into the second textbox



<script type="text/javascript">
window.onload = function() {
document.getElementById('idOfFirstTextbox').onchange = function() {
var firstvalue = parseFloat(this.value);
document.getElementById('idOfSecondTextbox').value = (firstvalue * 0.029 + 0.3).toFixed(2);
}
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum