Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Call Javascript function from another js function using variable name

Posted on 2010-09-02
6
Medium Priority
?
631 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

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 …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

618 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