Search code examples
jqueryeventstriggersbindjquery-events

Call jQuery trigger with jQuery object


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.


Solution

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