JQuery custom event data (subscription and trigger)

I am trying to figure out how I can set arguments for custom events. How can I set an argument when subscribing to an event, and then add some extra data when I fire the event.

I have a simple JS for testing, but in the “handle” e-parameter, I see only the subscription data.

function handle(e) {
    //e.data has only "b"
    alert(e.data);
}

function myObj() {
    this.raise = function () {
            //Trigger
        $(this).trigger("custom", { a: "a" });
    }
}

var inst = new myObj();
//Subscribe
$(inst).bind("custom", { b: "b" }, handle);
inst.raise();

Thank.

+5
source share
1 answer

The parameters passed to .trigger()are passed as the second parameter to the event handler function.

function handle(e, triggerParam) {
    //e.data has only "b"
    alert(e.data + ' also ' + triggerParam);
}
+5
source

All Articles