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

caculating values in JavaScript

P: 8
please see the script below

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE="JavaScript">
  2. function calculate() {
  3.     var total = 0;
  4.  
  5.     for (var i = 1; i < arguments.length; i++) {
  6.         total += parseInt(arguments[i].value, 10);
  7.     }
  8.  
  9.     document.forms.myForm._1_1_82_1.value = total;
  10.  
  11. </SCRIPT>
  12. <input type="button" value="Time lost to weather" onclick="calculate(0, this.form._1_1_44_1_84_1, this.form._1_1_44_1_101_1, this.form._1_1_44_1_102_1, this.form._1_1_44_1_106_1, this.form._1_1_44_1_110_1);">
  13.  
  14. </FORM>
this work fine if i click on the time lost to weather button, but want i would like to do is either poplulate the vaule dynamically or when i submit the form

can any one help
Feb 23 '10 #1

✓ answered by larztheloser

No - you make a submit button! Then in the form tag make sure that the action for onsubmit is "calculate();". Now it should all work automatically. The return true submits the form.

Share this Question
Share on Google+
16 Replies


Dormilich
Expert Mod 5K+
P: 8,639
your function call ( in <input>) is totally useless, because you donít read the values in the function (besides that they are defined wrong).

and I canít make sense of your problem description.
Feb 23 '10 #2

P: 8
sorry

Does this make more sense

basically i when i submit the form i would like a caculation to run and put the vaule in total field

My Script
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function calculate() {
  3.     var total = 0;
  4.  
  5.     for (var i = 1; i < arguments.length; i++) {
  6.         total += parseInt(arguments[i].value, 10);
  7.     }
  8.  
  9.     document.forms.myForm.myTotal.value = total;
  10. }
  11. </script>
  12.  
  13. <FORM NAME="myForm" >
  14. <input type="button" value="Calculate" onclick="calculate(0, this.form.myFld1, this.form.myFld2, this.form.myFld3,this.form.myFld4, this.form.myFld5);">
  15.  
  16.  
  17. <INPUT NAME="myFld1" TYPE="text" VALUE=""></INPUT>+
  18. <INPUT NAME="myFld2" TYPE="text" VALUE=""></INPUT>+
  19. <INPUT NAME="myFld3" TYPE="text" VALUE=""></INPUT>+
  20. <INPUT NAME="myFld5" TYPE="text" VALUE=""></INPUT>+
  21. <INPUT NAME="myFld4" TYPE="text" VALUE=""></INPUT>=
  22.  
  23. <INPUT NAME="myTotal" TYPE="text" VALUE=""></INPUT>
  24. </FORM>
  25.  
  26. </html>
  27.  
Feb 23 '10 #3

P: 8
Has anyone got any ideas on my issue ?
Feb 23 '10 #4

zorgi
Expert 100+
P: 431
Here is friendly jQuery solution:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="jquery.js"></script>
  2. <script type="text/javascript">
  3. $(function(){
  4.            $("form > input:button").click(function(event){
  5.                         var total = 0;
  6.                         $("form > input:not(:button)").each(function(){
  7.                                                     if(this.name != 'myTotal'){total = total + parseInt(this.value);}                                    
  8.                                                 })
  9.                         $("form > input[name$='myTotal']").value(total);                            
  10.                     })
  11.           })    
  12. </script>
  13.  
However you do need to remove onclick sausage (onclick="calculate(0, this.form.myFld1, this.fo........) and download jQuery.
Feb 23 '10 #5

gits
Expert Mod 5K+
P: 5,390
basicly the shown code in post is ok - the function uses the arguments parameter to iterate through the passed values. now to the submit - you might add the submit() to the calculation funtion or use the form's onsubmit handler ...

for such a basic and simple task i wouldn't suggest to use a lib like jQuery (which for such a purpose is certainly just overhead) ... it might be useful when you have more JavaScript tasks for that app ...

kind regards
Feb 23 '10 #6

zorgi
Expert 100+
P: 431
@gits
True.

Its 100% overkill but I just like using it. I just like the idea of getting anything on the page easy and without worry if it works in all browsers. I only know basics of javascript. I gave up on it ages ago when I came across first cross browser compatibility issues. Php saved the day. I started doing everything in php (or as much as i could) but with jQuery javascript is fun again and I do what I do firstly cos its fun :)))
Feb 23 '10 #7

P: 8
Hi Gits

I am new to Java can you please expain in more detail where i would need to add the submit or how i would use the forms onsubmit handler
Feb 23 '10 #8

gits
Expert Mod 5K+
P: 5,390
btw. JavaScript has very less to do with Java ...

just add the form-submit after your calculation ... in the function you have written.

kind regards
Feb 23 '10 #9

P: 8
Git

can you give me any examples

any help would be appericate
Feb 23 '10 #10

gits
Expert Mod 5K+
P: 5,390
?? ... did you have had a look at the link i showed above? ... the submit could be triggered by the submit() method of a form ... in your case it should look like:
Expand|Select|Wrap|Line Numbers
  1. document.forms.myForm.submit();
kind regards
Feb 24 '10 #11

P: 8
Expand|Select|Wrap|Line Numbers
  1. function calculate() { 
  2.     var total = 0; 
  3.  
  4.     for (var i = 1; i < arguments.length; i++) { 
  5.         total += parseInt(arguments[i].value, 10); 
  6.     } 
  7.  
  8.     document.forms.myForm.myTotal.value = total; 
  9.     document.forms["myform"].submit();
it this where it should put in my my script then ?
Feb 24 '10 #12

P: 86
Expand|Select|Wrap|Line Numbers
  1. function calculate() { 
  2.     var total = 0; 
  3.  
  4.     for (var i = 1; i < arguments.length; i++) { 
  5.         total += parseInt(arguments[i].value, 10); 
  6.     } 
  7.  
  8.     document.forms.myForm.myTotal.value = total;
  9.     return true;
  10. }
  11.  
Worked for me. I also got rid of the initial zero in the call to calculate() cause it's redundant, then made i==0 at the start of your FOR loop. Just good coding practice not to have redundant arguments.
Feb 24 '10 #13

P: 8
larztheloser

where in your code are you saying caculate onsubmit i can get it to work if i created a onclick button, but would like this to be done automatcally
Feb 24 '10 #14

P: 86
No - you make a submit button! Then in the form tag make sure that the action for onsubmit is "calculate();". Now it should all work automatically. The return true submits the form.
Feb 24 '10 #15

P: 8
have you got any examples of how you would do this as i am new to Java scripts

any help you can give me would be great
Feb 24 '10 #16

P: 86
Making a submit button is HTML code.

Navigate to the form tag. Add this to the end: onsubmit="calculate();".

That's about all (:
Feb 24 '10 #17

Post your reply

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