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
Solved

Call Javascript function from another js function using variable name

Posted on 2010-09-02
6
623 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
ID: 33589751
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33589839
0
 

Author Closing Comment

by:dgmoore1
ID: 33589995
eval(funcname) works; call() and apply() don't.  Many thanks!
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

838 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