Link to home
Start Free TrialLog in
Avatar of algomez
algomez

asked on

Help subtracting two numbers and showing result in form field.

I am working on form. The following is just a part of the form. The user will enter two numbers say Num1 and Num2. I want to actively show the result in a readonly text field. How can I do this using javascript? There are other fields in the form too so I don't want to lose their value if I have to reload. Any help will be appreciated. I am also using cold fusion.

Thanks!

Avatar of avner
avner

<html>
<head>
<title>new Documet </title>
<script>
<!--(c)Avner Cohen [avcoh@yahoo.com]

-->
</script>
<style>

</style>
</head>

<body>
<FORM>
<BUTTON onclick="this.form.value3.value=(this.form.value1.value-this.form.value2.value)">Calculate</BUTTON><BR/>
Value 1 :<input type="text" name="value1" value="1111">
<BR/>Value 2 :<input type="text" name="value2" value="563">
<BR/>Result  :<input type="text" name="value3" value="" DISABLED="TRUE" READONLY="TRUE">

</body>
</html>
Avatar of algomez

ASKER

This is very close to what I want but is there a away to not have to use the button to get the answer? The reason being that this subtraction is just part of a bigger form with other text fields.  If I am the user I will enter a value in the first num field , then enter the second number and when I click anywhere else on the form the readonly field displays the answer...?

Thanks alot!!!
Avatar of algomez

ASKER

This is very close to what I want but is there a away to not have to use the button to get the answer? The reason being that this subtraction is just part of a bigger form with other text fields.  If I am the user I will enter a value in the first num field , then enter the second number and when I click anywhere else on the form the readonly field displays the answer...?

Thanks alot!!!
ASKER CERTIFIED SOLUTION
Avatar of avner
avner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of algomez

ASKER

This is very close to what I want but is there a away to not have to use the button to get the answer? The reason being that this subtraction is just part of a bigger form with other text fields.  If I am the user I will enter a value in the first num field , then enter the second number and when I click anywhere else on the form the readonly field displays the answer...?

Thanks alot!!!
Avatar of algomez

ASKER

Thanks! Exactly what I need!

Try this out:

<html>
<head>
     <title>Untitled</title>
<script language="javascript">
function subtract(){
result.value = (num1.value - num2.value)
}
</script>
</head>

<body>
num1:<input type="text" name="num1" value="0"><br>
num2:<input type="text" name="num2" onChange="subtract()" value="0"><br>
otherField:<input type="text" name="other" value="click here"><br>
result:<input type="text" readonly name="result">
</body>
</html>
never mind.  I was typing while you must have accepted his answer.