dgmoore1
asked on
Call Javascript function from another js function using variable name
I'm sure this is trivially easy, but Javascript is not my strong suit.
I have a js file with numerous functions named ol1(), ol2(), ..., oln().
In this function on my page:
function onTaskSelect(e)
{
var funcname = 'ol' + e.id + '()'
?.......?
}
how do I call the function in the js file named funcname?
I have a js file with numerous functions named ol1(), ol2(), ..., oln().
In this function on my page:
function onTaskSelect(e)
{
var funcname = 'ol' + e.id + '()'
?.......?
}
how do I call the function in the js file named funcname?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
eval(funcname) works; call() and apply() don't. Many thanks!
That goes like this:
function onTaskSelect(e){
var funcname = 'ol' + e.id;
window[funcname]();
}
Attached the full tested code:
function onTaskSelect(e){
var funcname = 'ol' + e.id;
window[funcname]();
}
Attached the full tested code:
<script>
var myObj = {id:1};
onTaskSelect(myObj)
function onTaskSelect(e){
var funcname = 'ol' + e.id;
window[funcname](42);
}
function ol1(myParam){
alert(myParam);
}
</script>
ASKER
Thanks to all for your replies
You are welcome.
http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
http://stackoverflow.com/questions/496961/how-do-i-call-a-javascript-function-name-using-a-string
http://viralpatel.net/blogs/2009/01/calling-javascript-function-from-string.html