DaveEverFade Posted April 17, 2007 Share Posted April 17, 2007 How can I call a function dynamically? I assumed i could do it like this: function (param) { param() } ...but that doesn't work Any ideas? Dave Quote Link to comment Share on other sites More sharing options...
veridicus Posted April 17, 2007 Share Posted April 17, 2007 How are you passing a function to function(param)? Small example code which isn't working for you would help us answer. Quote Link to comment Share on other sites More sharing options...
DaveEverFade Posted April 17, 2007 Author Share Posted April 17, 2007 It's a very long and complex code but essentially I would pass the function name through another function onclick so eg: <img onclick="Test('Test2')"> function Test(params) { params() } function Test2() { do what I actually need to do.. } Quote Link to comment Share on other sites More sharing options...
veridicus Posted April 17, 2007 Share Posted April 17, 2007 You want to pass the function without the quotes. The function is an object, so pass it directly, rather than a string containing the function name. Quote Link to comment Share on other sites More sharing options...
DaveEverFade Posted April 17, 2007 Author Share Posted April 17, 2007 The problem is that I need the function name in quotes because it wont always be a function name. It may be a somthing like 'id=2' at the moment I have a rather large switch on it but it's just getting too big. Can I turn it back into an object maybe? Quote Link to comment Share on other sites More sharing options...
nogray Posted April 17, 2007 Share Posted April 17, 2007 you can use the eval function to run a the code stored in string eval(params+"();"); Quote Link to comment Share on other sites More sharing options...
DaveEverFade Posted April 18, 2007 Author Share Posted April 18, 2007 Thank you, you're a star! Dave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.