Solved

Help subtracting two numbers and showing result in form field.

Posted on 2002-07-29
8
216 Views
Last Modified: 2008-03-06
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!

0
Comment
Question by:algomez
  • 4
  • 2
  • 2
8 Comments
 
LVL 14

Expert Comment

by:avner
ID: 7184692
<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>
0
 

Author Comment

by:algomez
ID: 7184788
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!!!
0
 

Author Comment

by:algomez
ID: 7184789
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!!!
0
 
LVL 14

Accepted Solution

by:
avner earned 100 total points
ID: 7184797
Use this to make it work when you click anywhere on the screen :
<html>
<head>
<title>new Documet </title>
<script>
<!--(c)Avner Cohen [avcoh@yahoo.com]

-->
</script>
<style>

</style>
</head>

<body onclick="document.forms[0].value3.value=(document.forms[0].value1.value-document.forms[0].value2.value)">
<FORM>
<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>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:algomez
ID: 7185500
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!!!
0
 

Author Comment

by:algomez
ID: 7185510
Thanks! Exactly what I need!

0
 
LVL 5

Expert Comment

by:CFXPERT
ID: 7185543
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>
0
 
LVL 5

Expert Comment

by:CFXPERT
ID: 7185548
never mind.  I was typing while you must have accepted his answer.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now