homepage Welcome to WebmasterWorld Guest from 54.237.78.165
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
calling a function from another function
mylungsarempty

10+ Year Member



 
Msg#: 1361 posted 6:42 pm on Mar 1, 2004 (gmt 0)

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.

 

Purple Martin

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1361 posted 9:47 pm on Mar 1, 2004 (gmt 0)

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();"

mylungsarempty

10+ Year Member



 
Msg#: 1361 posted 1:23 pm on Mar 2, 2004 (gmt 0)

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.

Purple Martin

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1361 posted 10:05 pm on Mar 2, 2004 (gmt 0)

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);
}

mylungsarempty

10+ Year Member



 
Msg#: 1361 posted 3:15 pm on Mar 3, 2004 (gmt 0)

<moved post>

mylungsarempty

10+ Year Member



 
Msg#: 1361 posted 7:53 pm on Mar 3, 2004 (gmt 0)

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>

Purple Martin

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1361 posted 5:10 am on Mar 4, 2004 (gmt 0)

I think loop is a reserved word. Choose something else, like myLoop.

mylungsarempty

10+ Year Member



 
Msg#: 1361 posted 1:23 pm on Mar 4, 2004 (gmt 0)

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

nobody

10+ Year Member



 
Msg#: 1361 posted 4:26 pm on Mar 4, 2004 (gmt 0)

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 ;)

Purple Martin

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1361 posted 1:45 am on Mar 5, 2004 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved