0

The code below generates click functions for each given ID in the single-line array, using the uncommented code, I get the error "string is not a function". If I uncomment the commented code, and comment the single-line array, it does work well. However I prefer the approach with the singe-line array, to me, for obvious reasons.

Can anyone give me some good advice? Am I on the right path?

Thanks in advance.

// callme
function callme() {
    alert("call me");
    return true;
}

// create javascript object
//var adapter = {};
// set values - key contains: ID associated with link, button or tab - value contains:   the function to call
//adapter['callme'] = callme;
//adapter['callme1'] = callme1;
//adapter['callme2'] = callme2;
//adapter['callme3'] = callme3;

var adapter = ["callme", "callme1", "callme2", "callme3"];

// foreach the object - key as ID - value as associated function
$.each(adapter, function(index, value) {
    // click on listed ID
  $("#"+ value).click(function() {
    // call associated function
        value();    
    });
    // end foreach
});
Maarten Hartman
  • 1,611
  • 6
  • 26
  • 45
  • 1
    see this question: http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call – Schleis Apr 12 '13 at 13:31
  • is everyone absolutely sure this is a duplicate? this one is far from duplicate: http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call and this one: http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string - I dont understand. Function name between quotes? Arguments? Please elaborate someone – Maarten Hartman Apr 12 '13 at 13:45
  • If I use: window["value"](); I get the error: "Uncaught TypeError: Object [object global] has no method 'value' " – Maarten Hartman Apr 12 '13 at 13:47
  • Then do `self[value]();` – Paul Apr 12 '13 at 15:45
  • tried, returns same error – Maarten Hartman Apr 15 '13 at 07:35

2 Answers2

3

It depends a bit on the scope, but the general idea is that you can use the square bracket notation to get a reference to the function and execute it. For example, the following:

window[value]();

would be equivalent to callme() for the first iteration, assuming the callme function is globally scoped (a property of the window object).

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
  • thanks, I've already tried that, I got the error "Uncaught TypeError: Property 'callme' of object [object global] is not a function" - so the callme function is probably not globally scoped. How should I do that? – Maarten Hartman Apr 12 '13 at 13:34
  • @MaartenHartman I'd need to see more of your code to determine the correct scope, or you can move it outside of any closures (so just inside the `` tags) so it *is* globally scoped. – Anthony Grist Apr 12 '13 at 13:36
  • the code displayed above is all there is (except for $(document).ready(function($) {). Without – Maarten Hartman Apr 12 '13 at 13:38
0

You could try using eval to evaluate the string as a function, although you have to be careful security-wise. Evaluating a string coming from user input or a form submission could make your site vulnerable to cross-site scripting and session hijacking.

Example:

eval(value + "();"); //call "value" as a function
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187