Thursday, 6 June 2013

Explain the use of the $.fn.bind and $.fn.trigger.

Both the $.fn.bind and $.fn.triggers are two important jquery methods. They are primarily used with custom events.
- $.fn.bind: This method accepts an event type and an event handling function as an argument for itself. This method can also accept event-related data as a second argument.
- $.fn.trigger: This method can accept an event type as an argument. It can also take an array of values.

Ex. depicting the usage of $.fn.bind and $.fn.triggers using custom data in both the cases:

$(document).bind('myCustomEvent', { foo : 'bar' },
function(e, arg1, arg2) {
console.log(e.data.foo); // 'bar'
console.log(arg1); // 'bim'
console.log(arg2); // 'baz'
});

$(document).trigger('myCustomEvent', [ 'bim', 'baz' ]);

No comments:

Post a Comment