please see the script below - <SCRIPT LANGUAGE="JavaScript">
-
function calculate() {
-
var total = 0;
-
-
for (var i = 1; i < arguments.length; i++) {
-
total += parseInt(arguments[i].value, 10);
-
}
-
-
document.forms.myForm._1_1_82_1.value = total;
-
-
</SCRIPT>
-
<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);">
-
-
</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
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.
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 -
<script type="text/javascript">
-
function calculate() {
-
var total = 0;
-
-
for (var i = 1; i < arguments.length; i++) {
-
total += parseInt(arguments[i].value, 10);
-
}
-
-
document.forms.myForm.myTotal.value = total;
-
}
-
</script>
-
-
<FORM NAME="myForm" >
-
<input type="button" value="Calculate" onclick="calculate(0, this.form.myFld1, this.form.myFld2, this.form.myFld3,this.form.myFld4, this.form.myFld5);">
-
-
-
<INPUT NAME="myFld1" TYPE="text" VALUE=""></INPUT>+
-
<INPUT NAME="myFld2" TYPE="text" VALUE=""></INPUT>+
-
<INPUT NAME="myFld3" TYPE="text" VALUE=""></INPUT>+
-
<INPUT NAME="myFld5" TYPE="text" VALUE=""></INPUT>+
-
<INPUT NAME="myFld4" TYPE="text" VALUE=""></INPUT>=
-
-
<INPUT NAME="myTotal" TYPE="text" VALUE=""></INPUT>
-
</FORM>
-
-
</html>
-
Has anyone got any ideas on my issue ?
zorgi 431
Recognized Expert Contributor
Here is friendly jQuery solution: -
<script type="text/javascript" src="jquery.js"></script>
-
<script type="text/javascript">
-
$(function(){
-
$("form > input:button").click(function(event){
-
var total = 0;
-
$("form > input:not(:button)").each(function(){
-
if(this.name != 'myTotal'){total = total + parseInt(this.value);}
-
})
-
$("form > input[name$='myTotal']").value(total);
-
})
-
})
-
</script>
-
However you do need to remove onclick sausage (onclick="calculate(0, this.form.myFld1, this.fo........) and download jQuery.
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
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 :)))
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
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
Git
can you give me any examples
any help would be appericate
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: - document.forms.myForm.submit();
kind regards
- function calculate() {
-
var total = 0;
-
-
for (var i = 1; i < arguments.length; i++) {
-
total += parseInt(arguments[i].value, 10);
-
}
-
-
document.forms.myForm.myTotal.value = total;
-
document.forms["myform"].submit();
it this where it should put in my my script then ?
-
function calculate() {
-
var total = 0;
-
-
for (var i = 1; i < arguments.length; i++) {
-
total += parseInt(arguments[i].value, 10);
-
}
-
-
document.forms.myForm.myTotal.value = total;
-
return true;
-
}
-
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.
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
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.
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
Making a submit button is HTML code.
Navigate to the form tag. Add this to the end: onsubmit="calculate();".
That's about all (:
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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) {...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |