PDA

View Full Version : JS: automatic calculate without click any button


cloudy
11-26-2007, 01:46 PM
halo..i got some javascript problem..i need some help.
i have a hidden form value which contain of the TOTAL., let say the total is rm100.
then i have anohter two radio button and two text box, one radio button for one text box.
for the first radio button & textbox set, the user should insert the percentage; for the second radio button and terxt box set, the user should insert the ringgit malaysia.
then how can i get the value form the hidden and the text box and radio button, it will automatically calcualte the discount rate witout submit any button.
let say i have the hidden total is rm100, then i choose ringgit radio button and insert rm50 into the text box, then below there will display rm50(rm100-rm50)?..plz help..plz guide me some coding..thanx.

Philip M
11-26-2007, 03:03 PM
Something like this?

<form name= "myform">
<p> Enter Percentage <input type = "text" name = "percentage"></p>
<p> Enter Currency Amount <input type = "text" name = "amount" onchange = "calculate()">
<p> Discounted Amount <input type = "text" name = "dAmount">
</form>

<script type = "text/javascript">
function calculate() {
x = document.myform.percentage.value/100;
y = document.myform.amount.value;
document.myform.dAmount.value = x * y;
}
</script>

You will need to validate the input data to ensure that it consists of numbers in an acceptable range.