Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: open

Message Too Old, No Replies

calling a function from another function

   
6:42 pm on Mar 1, 2004 (gmt 0)

10+ Year Member



I want the click of one button to execute the same function for a number of forms... Could I do this by calling on one function that calls on other functions (one for each form) that each call on the single function doing the calculating?

How would i execute one function from inside another function?

Each seperate form is exactly the same. The only difference is each of the forms pertain to a different person. I just need the same operations performed for each form.

9:47 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes you can write one function that calls many others:

function callLots() {
another1();
another2();
another3();
another4();
}

But it's much easier to just call them all from the button onClick:

onClick="another1();another2();another3();another4();"

1:23 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



now how can i have one function that i plug into a series of other functions where, for each function it's plugged into a different value is supplied for each variable, like

i have 50 identical forms on different <DIV>'s... I want to click one button to call on a different function for each div, each function inserting the name of the div or whatever into the calculating function, so that i can use this one function to calculate the individual results of each form and return these values. it's a time clock.

10:05 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you need a loop!

Here's an example of how to call one function (called myFunction) 50 times, each time passing it a different div id as an argument. The divs ids are called myDiv1, myDiv2, myDiv3 etc. It works by concatenating (sticking together with the plus sign) a string "myDiv" with the value of the counter (the variable called i).

myFunction(argument) {
alert(argument)
}

for (var i = 1; i <= 50; i++) {
var myDivName = "myDiv" + i;
myFunction(myDivName);
}

3:15 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



<moved post>
7:53 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



FOR SOME REASON, i cannot get this to work... when i change onClick="loop()" to onClick="calculate(document.forms[0])" then i have no problem. What is getting lost in the translation in this loop() function? am i supplying the argument incorrectly?

function loop(){
for (var i = 0; i <= 48; i++) {
var arg = "document.forms["+i+"]";
calculate(arg);
}
}

<FORM>
<input type="button" value="calculate" onClick="loop()"> //javascript error tells me that object does not support this action
</FORM>

5:10 am on Mar 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think loop is a reserved word. Choose something else, like myLoop.
1:23 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



I renamed the function. I still get the same error message )=
4:26 pm on Mar 4, 2004 (gmt 0)

10+ Year Member



Where is your javascript code block? - it might be in the wrong place. You might need to move it into the <head>..</head>

or, I could possibly be totally wrong ;)

1:45 am on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I renamed the function. I still get the same error message )=

When I renamed it, it worked perfectly. As nobody said*, put the JavaScript in the head, and make sure it's inside <script> tags.

* that sounds odd.