Livinus
Posts: 48
|
| Posted: 05/01/2009, 2:52 PM |
|
Hello all
I am new to javascript as I use server events mostly for my apps. Currently i have a need to change the value of some controls and display the result on the affected controls before submiting the form. I have a form with the following controls: total,rate,other_charges,discount , amount_paid & balance all related as follwows:
1. total=rate+other_charges+(discount/100*rate)
2. balance=total-amount_paid
I want to attach a script to the onchange event of each control to display the calculated values values on the form dynamically each time values change for the other controls before submitting the values for storage in the database
I would appreciate any help.
|
 |
 |
melvyn
Posts: 333
|
| Posted: 05/01/2009, 8:48 PM |
|
let's say your form is named Form1.
<form name="Form1" .... >
The onchange event on each control will look like this:
onchange="calculateTotal();"
Then let's do the function to calculate the total:
function calculateTotal(){
rate = document.forms['Form1'].rate.value;
other = document.forms['Form1'].other.value;
discount = document.forms['Form1'].discount.value;
total = rate + other + (discount/100*rate);
document.forms['Form1'].total.value = total;
amount_paid = document.forms['Form1'].amount_paid.value;
balance = total - amount_paid;
document.forms['Form1'] .balance.value = balance;
}
That's all. The code is self-explaining. It's as clear as plain language
_________________
Melvyn Perez
Puro Codigo
http://purocodigo.com |
 |
 |
Livinus
Posts: 48
|
| Posted: 05/04/2009, 4:34 PM |
|
Hi melvyn,
You've made my day. I am a newbie in javascript but your codes are just too clear for me to fail to grasp what it is doing. I however want to ask where to attach the function. I mean which event?. Is it the form's onload event or where exactly? Thanks for your assistance.
|
 |
 |
damian
Posts: 838
|
| Posted: 05/04/2009, 8:24 PM |
|
usually you will add the function in the page onload and then call it as required.
_________________
if you found this post useful take the time to help someone else.... :)
|
 |
 |
melvyn
Posts: 333
|
| Posted: 05/05/2009, 12:43 PM |
|
Quote Livinus:
I however want to ask where to attach the function. I mean which event?. Is it the form's onload event or where exactly? Thanks for your assistance.
Well, you want the function fired when a field change.
<input type="text" size="14" name="amount_paid" onchange="calculateTotal();" />
This is clear: the function will fire when the field amount_paid change its content.
If you reffer to a client side event from CCS don't care. Forget it and insert this in the field.
_________________
Melvyn Perez
Puro Codigo
http://purocodigo.com |
 |
 |
Livinus
Posts: 48
|
| Posted: 05/07/2009, 11:13 PM |
|
Thank you Melvyn & Damian I appreciate your posts. I am sure the codes will be useful to many others in future just as they have been to me.
|
 |
 |