Solved

Call Javascript function from another js function using variable name

Posted on 2010-09-02
6
620 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
6 Comments
 
LVL 8

Accepted Solution

by:
netmunky earned 500 total points
Comment Utility
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
0
 

Author Closing Comment

by:dgmoore1
Comment Utility
eval(funcname) works; call() and apply() don't.  Many thanks!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
Thanks to all for your replies
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You are welcome.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now