2

I'm trying to assign a click handler to a JQuery object, defined in a variable :

some.object.array[8].action = function(data){console.log(data);}
anotherobject = {..}

now inside some loop, I need to assign this function to the click handler: and want to pass the whole 'anotherobject' object

for (var i = 0; i < foo.length; i++) {
    $('<div/>').click(some.object.array[i].action);
}

But how can I pass the parameter? If I encapsulate it inside some anonymous function, I'm losing my scope...:

for (var i = 0; i < foo.length; i++) {
    $('<div/>').click(function() {
        some.object.array[i].action(anotherobject)
    });
}

because i has changed...

How are we supposed to do this?

3 Answers 3

3

There are just too many ways to do this:

for (var i = 0; i < foo.length; i++) {
    (function(i) {
        $('<div/>').click(function() {
            some.object.array[i].action(anotherobject);
        });
    })(i);
}

Or

for (var i = 0; i < foo.length; i++) {
    $('<div/>').data("i", i).click(function() {
            var i = $(this).data("i");
            some.object.array[i].action(anotherobject);
        });
    });
}

Or

function getClickHandler(callback, parameter) {
    return function() { callback(parameter); };
};
for (var i = 0; i < foo.length; i++) {
    $('<div/>').click(getClickHandler(some.object.array[i].action, anotherobject));
}
Sign up to request clarification or add additional context in comments.

1 Comment

implementation was a bit complicated, but after all, it works! Thanks, Any idea how you call this way of doing (method1)
0

If you want your action function to maintain the div as this and still accept the jQuery event object, you can use bind like this example:

function action(another, event){
    console.log(this, arguments);
}

$(function(){
    for (var i = 0; i<10; i++) {
        var anotherObject = "another"+i;
        var div = $('<div>'+i+'</div>');
        // force 'this' to be 'div.get(0)'
        // and 'arg0' to be 'anotherObject'
        div.click(action.bind(div.get(0),anotherObject));
        $("body").append(div);
    }
})

Example: https://jsfiddle.net/Lwe5b9cx/ You'll need to open the console to see the output, which should look like this:

Comments

0
for(var i = 0; i < foo.length; i++)
{
  $('<div/>').click(
    (
      return function(callback){
        callback(anotherobject)
      }
    )(some.object.array[i].action)
  );
}

3 Comments

callback is also out of scope once the anonymous function is executed
This will not work. When this loop ends, i will be outside the bounds of the array and some.object.array[i] will return undefined. Plus since you are attaching handlers inside a for loop, they will ALL use this undefined value.
Thanks, now it should be fixed

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.