Click to See Complete Forum and Search --> : Simple math when changing a form value?


briandunning
05-28-2003, 09:41 AM
Hi,
I have a simple form and I want to update a couple other fields when someone types a number into one of them. I couldn't find this at javascripts.internet.com so I thought I'd ask here. Here's my simplified example, with three fields a,b, and c:

[a] - user types a number in here
[b] - read-only, updates to show the same number in [a]
[c] - read-only, updates to show value [a] plus 100

Thanks, I hope that makes sense. I want this to happen onChange of [a] rather than by clicking a button.

Jona
05-28-2003, 09:48 AM
<script type="text/javascript">
<!--
function updateFields(fA, f){
f.B.value = parseInt(fA.value);
f.C.value = parseInt(fA.value) + 100;
}
//-->
</script></head><body>
<form name="f"><div>
<input type="text" name="A" onKeyPress="updateFields(this,this.form);"><br>
<input type="text" name="B" readonly><br>
<input type="text" name="C" readonly>
</div></form></body></html>


If you're using decimals, change all instances of parseInt() to parseFloat();

briandunning
05-28-2003, 09:55 AM
That's amazing. You did that in like 3 seconds, and it's perfect. THANK YOU!!!

Jona
05-28-2003, 09:56 AM
It's only perfect if you have no further quesitons. ;)

Jona