Solved

Help subtracting two numbers and showing result in form field.

Posted on 2002-07-29
8
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

732 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