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?
dgmoore1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dgmoore1Author Commented:
eval(funcname) works; call() and apply() don't.  Many thanks!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
ZvonkoSystems architectCommented:
That goes like this:

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>

Open in new window

0
 
dgmoore1Author Commented:
Thanks to all for your replies
0
 
ZvonkoSystems architectCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.