I'm trying to figure out, how I can set arguments for custom events. How I can set an argument when I subscribing the event and then add some additional data when i trigger event.
I have a simple JS for testing, but in e parameter of "handle" i see only data of subscribe.
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 you.
jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.
If you want to trigger click event without clicking a button manually, then you should use the click() method in Javascript. Example : click button on page load dynamically. For that we will create a button with an id my-btn . So that, we can select the button using the getElementById() method.
Parameters supplied to .trigger()
are passed as the second parameter of the event handler function.
function handle(e, triggerParam) {
//e.data has only "b"
alert(e.data + ' also ' + triggerParam);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With