1

I have a jQuery object which I'd like to pass to jQuery.trigger. The bound function should receive this object and do something with it. However, it seems like the bound function receives a DOM node instead of the full jQuery object which was passed to trigger.

Example:

var doSomething = function(event, object) {
    object.myPluginFunction(); 
}

$("#selector").bind("finished", doSomething);

var myObject = $("#table").myPlugin();
$("#selector").trigger("finished", myObject);

The problem is: in doSomething I cannot access object.myPluginFunction... The received object appears in fact not to be an object. Instead, it is a DOM node. It seems to me like jQuery strips it.

Is there a way to get access, without calling $.myPlugin or introducing callback?

I use jQuery 1.4.1, and cannot upgrade.

2 Answers 2

1

You need to invoke the trigger parameters in an array

$("#selector").trigger("finished", [myObject]);​

jsFiddle

Also, note that since this "finished" is custom event that you created, and therefore not associated with any UI (native) event, it is more correct to invoke using triggerHandler instead of trigger.

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

Comments

0

Another exemple:

var obj = (function() {
    var that = {};
    var name = "toto";

    that.getName = function() {
        return name;    
    };

    return that;
})();

$("#btnTest").bind("customEvent", function(evt, obj) {
    console.log(obj.t1.getName());
});

$("#btnTest").click(function() {
    $("#btnTest").trigger("customEvent", {t1: obj});
});

jsFiddle: http://jsfiddle.net/EJLwu/3/

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.