/    Sign up×
Community /Pin to ProfileBookmark

Calling a function based on the value of a JS variable

Hello all,

I’m creating a JS that needs to call a function, but the name of this function is variable. I have instantiated a JS class that stores the name of the function that needs to be called to a variable. I now wish to call this function but Im unsure how to do this.

code snippet:

var returnfunc // The name of the function that needs to be called

trying to run the function like:

returnfunc(activeRow.cells[0].innerHTML, activeRow.cells[1].innerHTML, activeRow.cells[2].innerHTML);

but failing miserably. I know that the variable is correct since I used:
alert(returnfunc); right befor the attemp to call the function and the variable name is properly set.

Any help on how to call a function based on a variable’s name would be greatly appreciated.

Rob

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@toicontienOct 06.2008 — Is [B]returnfunc[/B] a function object or a string? They way you are using it, returnfunc should be a function object. Just before you call returnfunc, insert this line of code:
alert( typeof(returnfunc) );
It should alert "function". If it doesn't, that's your problem.
Copy linkTweet thisAlerts:
@rnd_meOct 06.2008 — if the variable points to a global function:

[CODE]
window[returnfunc](activeRow.cells[0].innerHTML, activeRow.cells[1].innerHTML, activeRow.cells[2].innerHTML);[/CODE]
Copy linkTweet thisAlerts:
@DokOct 06.2008 — If f is a function you can assign it to a and call it with
[CODE]
function f() {
}

a = f;

a();
[/CODE]
Copy linkTweet thisAlerts:
@binro01authorOct 07.2008 — Thanks all. It looks like the variable is coming in as a type string. How do I pass to the javascript the value of returnfunc as a type function so that I can use your suggestions?
Copy linkTweet thisAlerts:
@rnd_meOct 07.2008 — Thanks all. It looks like the variable is coming in as a type string. How do I pass to the javascript the value of returnfunc as a type function so that I can use your suggestions?[/QUOTE]

yay, that was my guess, i win!

i showed you how to use a string to invoke a ref to a function in the past post.

is your function not a global?
Copy linkTweet thisAlerts:
@DokOct 07.2008 — How do I pass to the javascript the value of returnfunc as a type function so that I can use your suggestions?[/QUOTE]
Instead of
[CODE]return 'myFunction';[/CODE]
do
[CODE]return myFunction;[/CODE]
×

Success!

Help @binro01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...