1

I have an object that defines the name and parameters of a function to be called in the click event of a page element.

object_array = [
    {
       id: "id1",
       onclick: "load_file(filename.php);",
    },

    {
       id: "id2",
       onclick: "open_url('http://url.com');",
    }
];

That information must be retrieved dynamically. I'd like in the click event to call the onclick function for a given object in the array.

$('element').click(function() {
      // call object.onclick
});

Seems that eval() is not a good choice. Is there a different way of calling that function?

2
  • 1
    So object_array is retrieved from the server in JSON or similar? Or is it simply a literal embedded in your code? Commented May 16, 2012 at 0:48
  • Do you have any control over the objects in object_array? In other words, can you change the format of onclick into something reasonable? Commented May 16, 2012 at 0:53

3 Answers 3

2

You should refactor the object_array to:

[{
    id: "id1",
    action: "load_file",
    url: "filename.php"
}, {
   id: "id2",
   action: "open_url",
   url: 'http://url.com'
}];

Then you can call it with:

var actions = {
    load_file: function(url) {
        ...
    },
    open_url: function(url) {
        ...
    },
    ...
};
object_array.forEach(function(ob) {
    $("#"+ob.id).click(function() {
        actions[ob.action](ob.url);
    });
});

If you have more complex arguments, you could also deliver an arguments array instead of url and use apply() on the function.


Or, if you just want a lookup-table of functions, use:

var object = {
    "id1": function() {
        load_file('filename.php');
    },
    "id2": function() {
        open_url('http://url.com');
    }
};
$('element').click(function() {
    object[this.id]();
});
Sign up to request clarification or add additional context in comments.

1 Comment

The lookup-table of functions was enough for me. This was a good solution for me.
2

Here's a working jsfiddle: http://jsfiddle.net/Zh6Fv/1/

Like so:

 object_array = [
        {
           id: "id1",
           // Encapsulate in an anonymous function
           onclick: function(){ load_file('filename.php'); },
        },
        {
           id: "id2",
           // Encapsulate in an anonymous function
           onclick: function(){ open_url('http://url.com'); },
        }
    ];

Then actually bind it it like this ::

$('element').click(obj.onclick);

Presumably you would be using it like this::

object_array.forEach(function(obj){
     // Note there is no need to wrap the obj.onclick
     // into another anonymous function anymore!
     $("#" + obj.id).click(obj.onclick);
});

Here's a working jsfiddle: http://jsfiddle.net/Zh6Fv/1/

7 Comments

If I were to call the onclick inside a another function would this be function name() { object.onclick; }? Thanks.
if you want to call it, then you call it like function name() { object.onclick(); } just like you would a normal function! :)
My object array did not like the anonymous function within the object. It didn't like the onclick: function () {load_file('filename.php');},.
@user823527 I've added a jsfiddle link for you
This does work in the fiddle. But when called inside a function other than the click function, the object.onclick isn't done. function name() { object.onclick; } doesn't actually run the load_file() function.
|
0

you can declare a function like

onclick : function()
{
   action(arguments);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.