?
Solved

Call Javascript function from another js function using variable name

Posted on 2010-09-02
6
Medium Priority
?
628 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:dgmoore1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Accepted Solution

by:
netmunky earned 2000 total points
ID: 33589751
0
 

Author Closing Comment

by:dgmoore1
ID: 33589995
eval(funcname) works; call() and apply() don't.  Many thanks!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 33590000
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
 

Author Comment

by:dgmoore1
ID: 33590038
Thanks to all for your replies
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 33591532
You are welcome.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question