I am not sure if this is possible. I would like to pass in a function name as parameter like this
loadContent("http://test.com/", specialFunction);
specialFucntion is just a string :
function loadContent(href, functionIWant){
$.ajax({
type: "GET",
url: href,
dataType: "json",
success: function(res, textStatus, xhr) {
helper();
functionIWant + "()"; //So this would be treated as function
//and it actually calls specialFunction()
//is it possible for this?
}
});
}
How do I achieve this?
ADD-ON
Let’s say, I would pass in an array of function names, how do I call it?
for(var i = 0; i < functionIWant.length; i++){
functionIWant[i]; // how? appeciate a lot :)
}
You can just do it with
functionIWant()Example using your provided snippet:
For your addendum
Assuming the following three functions you want to call
The best I can recommend if you’re passed an array of function names as strings is to follow the advice here. Basically, you could just do it like this:
If, however, FunctionIWant is an array of actual functions, for example, you can simply iterate over them and call them individually like so:
In most cases, you’ll be better off assigning the function rather than as a string