...

View Full Version : Return dynamic function within script



Keltoi
10-13-2008, 06:48 PM
Hi there, hopefully someone can help me with a little problem which has been vexing me for a while.
Basically, I need to run a dynamic function name based on supplied variables.
Say I have function1.setA(), function2.setA() and function3.setA()
so something like
function runme(num){
return function+num+.setA()
}
onclick="runme(1);"I know this won't work, but you get the idea.

Ultimately I need to query an array of say 1,2,3 and run .setA if a match is found else run .setB (in the above example: function1.setA();function2.setB();function3.setB();)
As of yet, I don't know how I'm going to achieve this but don't mind figuring it out if someone can help me with the main query in this post - if you fancy helping with the second as well - then that'd be fantastic and much appreciated.

Many thanks for taking the time to read and I look forward to any responses.

Justin

A1ien51
10-13-2008, 07:02 PM
Well there is probably a better way of doing it if I saw your entire code, but without knowing the rest, set up a swicth statement



function test( num ){

switch(num){
case 1:
return foo1();
break;
case 2:
return foo2();
break;
case 3:
return foo3();
break;
}

}


Eric

A1ien51
10-13-2008, 07:06 PM
I guess you also can do this:



function foo1(){
alert('1');
}

function foo2(){
alert('2');
}

function test( num ){
window["foo" + num]();
}

test(2);
test(1);


Eric

Keltoi
10-13-2008, 08:55 PM
Sorry for my delayed response to your prompt reply

I think your latter suggestion is what I'm after however firebug is giving the error
window["collapse" + num + ".slidedown"] is not a function
function subMen(num){
window["collapse" + num + ".slidedown"]();
}I've tried replacing window with document to no avail.

Trinithis
10-13-2008, 10:29 PM
Consider redesigning your code to use an array. Then you wouldn't have to deal with messey string manipulations as suggested above.



var objects = [
{setA: function() {...}},
{setA: function() {...}},
{setA: function() {...}},
{setA: function() {...}}
];

objects[num].setA();

Keltoi
10-13-2008, 10:48 PM
thanks, I'll need to read up a bit more on arrays as I don't fully understand your suggestion.
At the moment I must admit I'm tempted to chicken out with the following:
if(num==1){action1.dothis();action2.dothat();}
if(num==2){faction1.dothat();action2.dothis();}

Keltoi
10-14-2008, 01:17 AM
Thanks for your help guys, for now I'm going to use the cop-out version I suggested in my last post and come back to it when I go though code clean-up. :thumbsup:

A1ien51
10-14-2008, 04:43 AM
Sorry for my delayed response to your prompt reply

I think your latter suggestion is what I'm after however firebug is giving the error
function subMen(num){
window["collapse" + num + ".slidedown"]();
}I've tried replacing window with document to no avail.

Because that is wrong

This would be a correct way to call it:


window["collapse" + num].slidedown();

Again, there has to be a better way of doing it. If you were to show us your code, we might be able to show you one.

Eric

Keltoi
10-14-2008, 10:23 PM
Hey Eric,
Sorry once again for the delay, I can only work on this project in the evenings at the moment.
Basically the code I'm using isn't intended for purpose which is why I'm messing with it. The original simply expands/collapses named div's and I'm trying to use it as part of my navigation.
I was just preparing an example for you when I checked the original source of the code, and what do you know... they've addressed this very thing in an update - lesson learned, always check for and run the latest version!

Anyhow, If you want to see what I was doing the code is here:
function subMen(num){
if(num==1){collapse1.slideit();swapBtn(1,2);collapse2.slideup();swapBtn(2,'');}
if(num==2){collapse1.slideup();swapBtn(1,'');collapse2.slideit();swapBtn(2,2);}
}However it might be easier to show you what I was doing in a live environment and you can access all the JS. Head on over to http://test.waddingtonairshow.co.uk/test.asp
The updated code which should fix most of my woes can be found at www.dynamicdrive.com/dynamicindex17/animatedcollapse.htm

Feel free to make any suggestions as I believe the day you stop learning or taking advice is a very sorry day. However, please don't feel any obligation, the code I have and indeed will have works, so I won't be hitting my head against the wall in frustration tonight...

Many thanks (and sorry for this mini essay...)

---------------

update
I think I might revert to the original as the new script seems to be affecting my little swap image function and more importantly, it doesn't allow for dynamic heights of the hidden div.
my test of this second method is at http://test.waddingtonairshow.co.uk/test2.asp

Once again, suggestions will of course be gratefully received, but I am more than happy to go with my first test - I know it could possibly be sweeter, but it works...

Thanks again

Justin

Keltoi
10-16-2008, 09:05 AM
just realised that because I updated my last post, you may not have been notified of the update... Mind you, I did say that I wasn't necessarily expecting a reply - so don't know why I'm writing this really... :confused:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum