By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,630 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,630 IT Pros & Developers. It's quick & easy.

Using document.onkeyup to calculate a field value

P: 8
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
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
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

P: 8
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
Expert Mod 15k+
P: 16,027
You forgot the parentheses on line 3:
Expand|Select|Wrap|Line Numbers
  1. function calcNow() {
Jun 15 '07 #4

P: 8
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
Expert Mod 5K+
P: 5,293
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

P: 8
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

Post your reply

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