4

Following code for passing an object to event handler is not working:

$('a.another').live('click', function(e, data) {
        alert(data); // **alerts '[{isMachineClick:true}]'**
        alert(data.isMachineClick); // **alerts 'undefined'**
});

$('a.another').trigger('click', "[{isMachineClick:true}]");

Please have a look at this.

PS: solution provided at link pass an object through jquery trigger is not working, so posting a new thread.

1 Answer 1

6

You're passing only a string, and what's more the JSON inside the string is an array, not an object. Try this:

$('a.another').live('click', function(e, data) {
    alert(data[0].isMachineClick);
});

$('a.another').trigger('click', [{isMachineClick:true}]);

UPDATE: Didn't realize how this worked: using an array is correct, and each additional item becomes another argument. This is the correct code:

$('a.another').live('click', function(e, data, data2) {

    alert(data.isMachineClick);

    alert(data2.someOtherThing);
});

$('a.another').trigger('click', [{isMachineClick:true}, {someOtherThing:false}]);
Sign up to request clarification or add additional context in comments.

2 Comments

hey Luke, thxs for the reply. I tested your code, no alert is coming. please check it. I will really appreciate if you point me an example showing how to pass an array, object and JSON to an event handler.
With the most recent version of jQuery (at least 1.x), can you not replace the .live() method with .bind() now?

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.