473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

caculating values in JavaScript

8 New Member
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.

16 1528
Dormilich
8,658 Recognized Expert Moderator Expert
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
neil1
8 New Member
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
neil1
8 New Member
Has anyone got any ideas on my issue ?
Feb 23 '10 #4
zorgi
431 Recognized Expert Contributor
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
5,390 Recognized Expert Moderator Expert
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
431 Recognized Expert Contributor
@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
neil1
8 New Member
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
5,390 Recognized Expert Moderator Expert
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
neil1
8 New Member
Git

can you give me any examples

any help would be appericate
Feb 23 '10 #10
gits
5,390 Recognized Expert Moderator Expert
?? ... 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
neil1
8 New Member
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
larztheloser
86 New Member
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
neil1
8 New Member
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
larztheloser
86 New Member
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
neil1
8 New Member
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
larztheloser
86 New Member
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

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

Similar topics

5
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the...
9
by: Ken | last post by:
How can I reset the initial form variables that are set with session statements when clicking on a button? I tried this but the function was not called: <?PHP function reset_form($none) {...
14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
8
by: savvy | last post by:
I have a javascript menu navigation. I have some session values which regularly get updated, but when i navigate using this Javascript menu to a different page i'm not able to see the updated...
5
by: Priya | last post by:
Hey all, I have some values in the database which has to retrieved and then passed on to some functions in the javascript. The javascript file is a separate file. I have included it in the aspx...
4
by: noddy | last post by:
I have a mysql table called Users The userID field values start at 100 and increment up from there I am trying to transfer values from this table into a javascript array Here is what the code...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
8
by: BigZero | last post by:
Hello, i need to pass some variables or value to javascript from php script. well all i need to pass to value to javascript that is with in same php file the two values r in php i need to pass...
7
by: vinodsk101 | last post by:
Hi all, I have one doubt, can we pass a variable from javascript to servlet??? In Brief, i am getting some values in javascript through jsp and the same values i want in servlet. Ya i know i...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.