473,503 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using document.onkeyup to calculate a field value

8 New Member
I have a form with 4 text input fields. The last one is the total of the previous three fields and should automatically calculate this value when a user enters data into any of the first 3.

Normally I would put an onkeyup="" event on the input elements and have that call the function. However, this form is automatically generated based upon information in an XML file. I can add some HTML and Javascript to the page via the XML file, but cannot directly modify the form elements. I am wondering if there is a way to run the calcIT() function below on every onkeyup event. The code below does not seem to be working. I know the calcIT function works correctly.

Expand|Select|Wrap|Line Numbers
  1. document.onkeyup = calcIT(document.quizform.userans0.value, document.quizform.userans1.value, document.quizform.userans2.value);
  2.  
  3. function calcIT(v,a,b) {
  4.  
  5. var total, v2, a2, b2;
  6. v2 = parseFloat(v)
  7. a2 = parseFloat(a)
  8. b2 = parseFloat(b)
  9. total = (v2 + a2 + b2);
  10.  
  11. document.quizform.userans3.value = total;
  12.  
  13. }
Jun 14 '07 #1
6 5237
acoder
16,027 Recognized Expert Moderator MVP
Welcome to TSDN!

Put the call to calcIt into another function, say calcNow, and then call:
Expand|Select|Wrap|Line Numbers
  1. window.onkeyup=calcNow;
Jun 14 '07 #2
kodt
8 New Member
Welcome to TSDN!

Put the call to calcIt into another function, say calcNow, and then call:
Expand|Select|Wrap|Line Numbers
  1. window.onkeyup=calcNow;
This does not appear to work either. Below is my updated code. I also tried putting an alert box in calcNow for testing, and it did not show up. Thanks.

Expand|Select|Wrap|Line Numbers
  1. window.onkeyup=calcNow;
  2.  
  3. function calcNow {
  4. calcIT(document.quizform.userans0.value, document.quizform.userans1.value, document.quizform.userans2.value);
  5. }
  6.  
  7. function calcIT(v,a,b) {
  8.  
  9. var total, v2, a2, b2;
  10. v2 = parseFloat(v)
  11. a2 = parseFloat(a)
  12. b2 = parseFloat(b)
  13. total = (v2 + a2 + b2);
  14.  
  15. document.quizform.userans3.value = total;
  16.  
  17. }
Jun 14 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
You forgot the parentheses on line 3:
Expand|Select|Wrap|Line Numbers
  1. function calcNow() {
Jun 15 '07 #4
kodt
8 New Member
You forgot the parentheses on line 3:
Expand|Select|Wrap|Line Numbers
  1. function calcNow() {
Thanks that worked.

I find that most of my javascript errors are syntax errors that I overlook :\
Jun 15 '07 #5
gits
5,390 Recognized Expert Moderator Expert
Thanks that worked.

I find that most of my javascript errors are syntax errors that I overlook :\
... to avoid it you may use an editor that checks such things (personally i use eclipse with the jseclipse-plugin) ... or to find that errors you may use the firebug-extension for firefox ... really good tools for js-development ...

kind regards ...
Jun 15 '07 #6
kodt
8 New Member
Replaced window.onkeyup with the following code to work in IE.

Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2. document.getElementById('userans0').onkeyup = calcNow;
  3. document.getElementById('userans1').onkeyup = calcNow;
  4. document.getElementById('userans2').onkeyup = calcNow;
  5. }
  6.  
Jun 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3994
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
4777
by: Greg | last post by:
I have the following code (a one line function) I'm trying to calculate a value for text field matPerPiece# where # is a row number I provide. The value is calculated from text fields...
13
9585
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
4
14816
by: engwar | last post by:
I'd like to know if it's possible to change the contents of a div tag based on something the user is doing. For example. If there is a text box on a page and the user types his or her name can I...
13
14359
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on...
5
11939
by: jdzemke | last post by:
I am getting 'options' is null or not an object using when using a dynamic dataset with a dropdown list in an html form. I am filling-in a text field and filtering the values in the drop-down. ...
106
19658
by: bonneylake | last post by:
Hey Everyone, Well i don't know if my question should be in javascript/ajax or coldfusion, i figure this is more of a coldfusion question. But if this is in the wrong section let me know an all...
1
1253
by: rubenhan | last post by:
Hi, I'm trying to use closure to pinpoint a form from a list of forms with the same name to erase its onblur function when the chosen form goes onblur. For example, there will be indefinite...
4
3099
by: sureshl | last post by:
function cal() { var f = document.form1; var regExp_Count = new RegExp("^+$"); f.price1.value = parseFloat(f.baseprice.value*(f.percen.value/100)).toFixed(0); } cal() functions , will...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.