Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Return dynamic function within script

    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
    Code:
    function runme(num){
      return function+num+.setA()
    }
    Code:
    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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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

    Code:
    function test( num ){
    
      switch(num){
        case 1:
          return foo1();
        break;    
        case 2:
          return foo2();
        break;
        case 3:
          return foo3();
        break;    
      }
    
    }
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I guess you also can do this:

    Code:
    	  function foo1(){
            alert('1');
    	  }
    
    	  function foo2(){
            alert('2');
    	  }
    
    	  function test( num ){
    	    window["foo" + num]();
    	  }
    
    	  test(2);
    	  test(1);
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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
    Code:
    function subMen(num){
    window["collapse" + num + ".slidedown"]();
    }
    I've tried replacing window with document to no avail.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Consider redesigning your code to use an array. Then you wouldn't have to deal with messey string manipulations as suggested above.
    Code:
    var objects = [
      {setA: function() {...}},
      {setA: function() {...}},
      {setA: function() {...}},
      {setA: function() {...}}
    ];
    
    objects[num].setA();
    Trinithis

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:
    Code:
    if(num==1){action1.dothis();action2.dothat();}
    if(num==2){faction1.dothat();action2.dothis();}

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Quote Originally Posted by Keltoi View Post
    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
    Code:
    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:
    Code:
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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 http://www.dynamicdrive.com/dynamici...edcollapse.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
    Last edited by Keltoi; 10-14-2008 at 11:38 PM. Reason: further tests

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    Lincoln (UK)
    Posts
    138
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •