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
});
5
  • 1
    see this question: stackoverflow.com/questions/912596/… Commented Apr 12, 2013 at 13:31
  • is everyone absolutely sure this is a duplicate? this one is far from duplicate: stackoverflow.com/questions/912596/… and this one: stackoverflow.com/questions/359788/… - I dont understand. Function name between quotes? Arguments? Please elaborate someone Commented Apr 12, 2013 at 13:45
  • If I use: window["value"](); I get the error: "Uncaught TypeError: Object [object global] has no method 'value' " Commented Apr 12, 2013 at 13:47
  • Then do self[value](); Commented Apr 12, 2013 at 15:45
  • tried, returns same error Commented Apr 15, 2013 at 7:35

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

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?
@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 <script> and </script> tags) so it is globally scoped.
the code displayed above is all there is (except for $(document).ready(function($) {). Without <script> tags, and loaded into the header.
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

2 Comments

thanks, I've found this suggestion as well, but nearly everyone said: do not use eval! I've tried it, and it indeed works well. But I prefer a safer solution.
As long as you are sure that you are not parsing user input, it is safe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.